<?php

declare(strict_types=1);

namespace App\Http\Requests\Pwa;

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

class UpdateClientRequest extends FormRequest
{
    public function authorize(): bool
    {
        // Autorizacion real en el controlador (canEditClients + visibilidad).
        return true;
    }

    public function rules(): array
    {
        $companyId = $this->user()->company_id;
        $clientId = (int) $this->route('id');

        return [
            'name' => ['required', 'string', 'max:255'],
            'identification' => [
                'required',
                'string',
                'max:50',
                Rule::unique('clients', 'identification')
                    ->where('company_id', $companyId)
                    ->ignore($clientId),
            ],
            'phone' => ['required', 'string', 'max:20'],
            'email' => ['nullable', 'email', 'max:255'],
            'state' => ['nullable', 'string', 'max:100'],
            'city' => ['nullable', 'string', 'max:100'],
            'address' => ['nullable', 'string', 'max:500'],
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => 'El nombre es obligatorio.',
            'identification.required' => 'La cedula/identificacion es obligatoria.',
            'identification.unique' => 'Ya existe un cliente con esta cedula.',
            'phone.required' => 'El telefono es obligatorio.',
            'email.email' => 'El email no es valido.',
        ];
    }
}
