<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class SubmitEmployeeDetailsRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'token' => ['required', 'string', 'size:64'],

            // RSA-OAEP encrypted NIN (same constraints as SubmitNinRequest)
            'encrypted_nin' => [
                'required',
                'string',
                'min:300',
                'max:400',
                'regex:/^[A-Za-z0-9+\/]*={0,2}$/',
            ],

            // Beneficiaries — at least one required, allocations must sum to 100
            'beneficiaries' => ['required', 'array', 'min:1'],
            'beneficiaries.*.first_name' => ['required', 'string', 'max:100'],
            'beneficiaries.*.last_name' => ['required', 'string', 'max:100'],
            'beneficiaries.*.date_of_birth' => ['required', 'date', 'before:today'],
            'beneficiaries.*.address' => ['required', 'string', 'max:255'],
            'beneficiaries.*.percentage_allocation' => ['required', 'numeric', 'min:1', 'max:100'],
        ];
    }

    public function messages(): array
    {
        return [
            'token.required' => 'A submission token is required.',
            'token.size' => 'Invalid token format.',
            'encrypted_nin.required' => 'Encrypted NIN is required.',
            'encrypted_nin.min' => 'Invalid encrypted NIN format.',
            'encrypted_nin.max' => 'Invalid encrypted NIN format.',
            'encrypted_nin.regex' => 'Invalid encrypted NIN format.',
            'beneficiaries.required' => 'At least one beneficiary is required.',
            'beneficiaries.min' => 'At least one beneficiary is required.',
            'beneficiaries.*.first_name.required' => 'Beneficiary first name is required.',
            'beneficiaries.*.last_name.required' => 'Beneficiary last name is required.',
            'beneficiaries.*.date_of_birth.required' => 'Beneficiary date of birth is required.',
            'beneficiaries.*.date_of_birth.before' => 'Beneficiary date of birth must be in the past.',
            'beneficiaries.*.address.required' => 'Beneficiary address is required.',
            'beneficiaries.*.percentage_allocation.required' => 'Beneficiary percentage allocation is required.',
            'beneficiaries.*.percentage_allocation.min' => 'Percentage allocation must be at least 1.',
            'beneficiaries.*.percentage_allocation.max' => 'Percentage allocation cannot exceed 100.',
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $beneficiaries = $this->input('beneficiaries', []);
            $total = collect($beneficiaries)->sum('percentage_allocation');

            if (round($total, 2) !== 100.00) {
                $validator->errors()->add(
                    'beneficiaries',
                    'Total beneficiary percentage allocation must equal 100%. Current total: ' . $total . '%.'
                );
            }
        });
    }
}
