<?php

namespace App\Http\Requests;

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

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            // BIO-DATA INFORMATION
            'title' => ['sometimes', 'nullable', 'string', 'max:20'],
            'first_name' => ['sometimes', 'string', 'max:100'],
            'last_name' => ['sometimes', 'string', 'max:100'],
            'middle_name' => ['sometimes', 'nullable', 'string', 'max:100'],
            'gender' => ['sometimes', 'nullable', 'string', Rule::in(['Male', 'Female', 'Other'])],
            'date_of_birth' => ['sometimes', 'date', 'before:today'],
            'nationality' => ['sometimes', 'string', 'max:100'],
            'phone_number' => ['sometimes', 'string', 'max:20'],
            'foreign_number' => ['sometimes', 'nullable', 'string', 'max:20'],
            'email_address' => ['sometimes', 'string', 'email', 'max:255'],
            'country' => ['sometimes', 'string', 'max:100'],
            'state' => ['sometimes', 'string', 'max:100'],
            'city' => ['sometimes', 'string', 'max:100'],
            'chn' => ['sometimes', 'nullable', 'string', 'max:50'],
            'house_address' => ['sometimes', 'string'],
            'previous_address' => ['sometimes', 'nullable', 'string'],

            // BANK INFORMATION
            'bank_name' => ['sometimes', 'nullable', 'string', 'max:100'],
            'bank_account_number' => ['sometimes', 'nullable', 'string', 'max:20'],
            'tin_or_pin' => ['sometimes', 'nullable', 'string', 'max:50'],
            'bank_details_consent' => ['sometimes', 'nullable', 'boolean'],

            // IDENTITY INFORMATION
            'identity_card_type' => [
                'sometimes',
                'string',
                Rule::in([
                    'National Identity Number',
                    'National ID',
                    'International Passport',
                    'Driver\'s License',
                    'Voter\'s Card',
                    'Permanent Voter\'s Card',
                ]),
            ],
            'identity_card_number' => ['sometimes', 'nullable', 'string', 'max:50'],

            // File uploads (optional for updates)
            'identity_card' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:pdf,jpg,jpeg,png',
                'max:5120',
            ],
            'scanned_signature' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:pdf,jpg,jpeg,png',
                'max:5120',
            ],
            'passport_photograph' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:jpg,jpeg,png',
                'max:5120',
            ],
            'nin_document' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:pdf,jpg,jpeg,png',
                'max:5120',
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'date_of_birth.before' => 'Date of birth must be before today.',
            'email_address.email' => 'Please provide a valid email address.',
            'identity_card_type.in' => 'Invalid identity card type selected.',
            'identity_card.mimes' => 'Identity card must be a PDF, JPG, JPEG, or PNG file.',
            'identity_card.max' => 'Identity card file size must not exceed 5MB.',
            'scanned_signature.mimes' => 'Scanned signature must be a PDF, JPG, JPEG, or PNG file.',
            'scanned_signature.max' => 'Scanned signature file size must not exceed 5MB.',
            'passport_photograph.mimes' => 'Passport photograph must be a JPG, JPEG, or PNG file.',
            'passport_photograph.max' => 'Passport photograph file size must not exceed 5MB.',
            'nin_document.mimes' => 'NIN document must be a PDF, JPG, JPEG, or PNG file.',
            'nin_document.max' => 'NIN document file size must not exceed 5MB.',
        ];
    }
}
