<?php

declare(strict_types=1);

namespace App\Http\Requests\Pwa;

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

class StoreClientRequest extends FormRequest
{
    public function authorize(): bool
    {
        // Authorization checked in controller via canCreateClients()
        return true;
    }

    public function rules(): array
    {
        $companyId = $this->user()->company_id;

        return [
            'name' => ['required', 'string', 'max:255'],
            'identification' => [
                'required',
                'string',
                'max:50',
                Rule::unique('clients', 'identification')
                    ->where('company_id', $companyId),
            ],
            '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.',
            'name.max' => 'El nombre no puede exceder 255 caracteres.',
            'identification.required' => 'La cedula/identificacion es obligatoria.',
            'identification.unique' => 'Ya existe un cliente con esta cedula.',
            'identification.max' => 'La cedula no puede exceder 50 caracteres.',
            'phone.required' => 'El telefono es obligatorio.',
            'phone.max' => 'El telefono no puede exceder 20 caracteres.',
            'email.email' => 'El email no es valido.',
            'state.max' => 'El departamento no puede exceder 100 caracteres.',
            'city.max' => 'La ciudad no puede exceder 100 caracteres.',
            'address.max' => 'La direccion no puede exceder 500 caracteres.',
        ];
    }
}
