<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProveedorRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $proveedorId = $this->route('proveedor') ? $this->route('proveedor')->id : null;

        return [
            'razon_social' => ['required', 'string', 'max:200'],
            'ruc'          => [
                'required',
                'digits:11',
                Rule::unique('proveedores', 'ruc')->ignore($proveedorId)
            ],
            'direccion'    => ['nullable', 'string', 'max:255'],
            'telefono'     => ['nullable', 'string', 'max:20'],
            'email'        => ['nullable', 'email', 'max:150'],
            'contacto'     => ['nullable', 'string', 'max:100'],
            'activo'       => ['nullable', 'boolean'],
        ];
    }

    public function messages(): array
    {
        return [
            'ruc.unique' => 'Este RUC ya está registrado para otro proveedor.',
            'ruc.digits' => 'El RUC debe tener exactamente 11 dígitos.',
        ];
    }
}
