<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use App\Exceptions\ValidationException;

class UpdateClientRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $clientId = $this->route('client')->id ?? null;

        return [
            'first_name' => 'required|string|max:255|regex:/^[a-zA-Z\s]+$/',
            'last_name' => 'required|string|max:255|regex:/^[a-zA-Z\s]+$/',
            'id_number' => 'required|string|unique:clients,id_number,' . $clientId . '|regex:/^[0-9]{6,12}$/',
            'date_of_birth' => 'required|date|before:' . now()->subYears(18)->format('Y-m-d'),
            'gender' => 'required|in:male,female,other',
            'phone' => 'required|string|unique:clients,phone,' . $clientId . '|regex:/^[0-9+\-\s()]{10,20}$/',
            'email' => 'nullable|email|unique:clients,email,' . $clientId . '|max:255',
            'address' => 'required|string|max:500',
            'city' => 'required|string|max:255',
            'country' => 'nullable|string|max:255',
            'occupation' => 'nullable|string|max:255',
            'monthly_income' => 'nullable|numeric|min:0|max:10000000',
            'next_of_kin_name' => 'nullable|string|max:255',
            'next_of_kin_phone' => 'nullable|string|max:20',
            'next_of_kin_relationship' => 'nullable|string|max:255',
            'photo' => 'nullable|image|max:2048|mimes:jpeg,png,jpg,gif',
            'id_document' => 'nullable|file|max:2048|mimes:pdf,jpeg,png,jpg',
            'notes' => 'nullable|string|max:1000',
            'status' => 'required|in:active,inactive,suspended,blacklisted',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'first_name.required' => 'Please enter the client\'s first name.',
            'first_name.regex' => 'First name can only contain letters and spaces.',
            'first_name.max' => 'First name cannot exceed 255 characters.',
            'last_name.required' => 'Please enter the client\'s last name.',
            'last_name.regex' => 'Last name can only contain letters and spaces.',
            'last_name.max' => 'Last name cannot exceed 255 characters.',
            'id_number.required' => 'Please enter the ID number.',
            'id_number.unique' => 'This ID number is already registered to another client.',
            'id_number.regex' => 'ID number must be 6-12 digits.',
            'date_of_birth.required' => 'Please enter the date of birth.',
            'date_of_birth.date' => 'Date of birth must be a valid date.',
            'date_of_birth.before' => 'Client must be at least 18 years old.',
            'gender.required' => 'Please select the gender.',
            'gender.in' => 'Gender must be male, female, or other.',
            'phone.required' => 'Please enter the phone number.',
            'phone.unique' => 'This phone number is already registered to another client.',
            'phone.regex' => 'Please enter a valid phone number.',
            'email.email' => 'Please enter a valid email address.',
            'email.unique' => 'This email address is already registered to another client.',
            'email.max' => 'Email address cannot exceed 255 characters.',
            'address.required' => 'Please enter the address.',
            'address.max' => 'Address cannot exceed 500 characters.',
            'city.required' => 'Please enter the city.',
            'city.max' => 'City name cannot exceed 255 characters.',
            'country.max' => 'Country name cannot exceed 255 characters.',
            'occupation.max' => 'Occupation cannot exceed 255 characters.',
            'monthly_income.numeric' => 'Monthly income must be a valid number.',
            'monthly_income.min' => 'Monthly income cannot be negative.',
            'monthly_income.max' => 'Monthly income cannot exceed KSh 10,000,000.',
            'next_of_kin_name.max' => 'Next of kin name cannot exceed 255 characters.',
            'next_of_kin_phone.max' => 'Next of kin phone number cannot exceed 20 characters.',
            'next_of_kin_relationship.max' => 'Relationship description cannot exceed 255 characters.',
            'photo.image' => 'Photo must be an image file.',
            'photo.max' => 'Photo size cannot exceed 2MB.',
            'photo.mimes' => 'Photo must be in JPEG, PNG, JPG, or GIF format.',
            'id_document.file' => 'ID document must be a file.',
            'id_document.max' => 'ID document size cannot exceed 2MB.',
            'id_document.mimes' => 'ID document must be in PDF, JPEG, PNG, or JPG format.',
            'notes.max' => 'Notes cannot exceed 1000 characters.',
            'status.required' => 'Please select the client status.',
            'status.in' => 'Status must be active, inactive, suspended, or blacklisted.',
        ];
    }

    /**
     * Handle a failed validation attempt.
     */
    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        throw new ValidationException(
            'Client update validation failed. Please check your input and try again.',
            'CLIENT_UPDATE_VALIDATION_FAILED',
            $validator->errors()->toArray()
        );
    }
}
