<?php

namespace App\Http\Requests;

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

class UpdateCompanyRegistrationRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true; // Authorization handled by middleware
    }

    public function rules(): array
    {
        $registration = $this->route('registration');

        return [
            // Company Information
            'company_name' => ['sometimes', 'string', 'max:255'],
            'rc_number' => ['sometimes', 'nullable', 'string', 'max:50'],
            'country' => ['sometimes', 'string', 'max:100'],
            'state' => ['sometimes', 'string', 'max:100'],
            'city' => ['sometimes', 'string', 'max:100'],
            'phone_number' => ['sometimes', 'string', 'max:20'],
            'secondary_phone' => ['sometimes', 'nullable', 'string', 'max:20'],
            'email_address' => [
                'sometimes',
                'string',
                'email',
                'max:255',
                Rule::unique('companies', 'email')->ignore($registration->company_id ?? null),
            ],
            'house_address' => ['sometimes', 'string'],
            'previous_address' => ['sometimes', 'nullable', 'string'],

            // Director Identity Information
            'director_name' => ['sometimes', 'string', 'max:255'],
            'identity_card_type' => [
                'sometimes',
                'string',
                Rule::in([
                    'National ID',
                    'National Identity Number',
                    '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)
            'director_identity_cards' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:pdf,jpg,jpeg,png',
                'max:5120', // 5MB
            ],
            'cac_document' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:pdf,jpg,jpeg,png',
                'max:5120', // 5MB
            ],
            'director_passport_photograph' => [
                'sometimes',
                'nullable',
                'file',
                'mimes:jpg,jpeg,png',
                'max:5120', // 5MB
            ],
        ];
    }

    public function messages(): array
    {
        return [
            'email_address.unique' => 'This email address is already in use.',
            'identity_card_type.in' => 'Invalid identity card type selected.',
        ];
    }
}
