<?php

namespace Modules\MembershipApplication\Http\Requests;

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

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

    /**
     * @return array<string, mixed>
     */
    public function validationData(): array
    {
        return $this->except(array_keys($this->route()?->parameters() ?? []));
    }

    /**
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'applicant' => 'sometimes|array',
            'applicant.name' => 'sometimes|required|string|max:255',
            'applicant.email' => 'sometimes|required|email|max:255',
            'applicant.ic_number' => 'sometimes|required|string|max:20',
            'applicant.birth_date' => 'sometimes|required|date',
            'applicant.birth_place' => 'sometimes|required|string|max:255',
            'applicant.gender' => ['sometimes', 'required', Rule::in(['Lelaki', 'Perempuan'])],
            'applicant.marriage_status' => 'sometimes|required|string|max:255',
            'applicant.address' => 'sometimes|required|string',
            'applicant.phone_number' => 'sometimes|required|string|max:20',
            'applicant.office_number' => 'nullable|string|max:20',
            'applicant.postcode' => 'sometimes|required|string|max:10',
            'applicant.employer_name' => 'sometimes|required|string|max:255',
            'applicant.employer_address' => 'sometimes|required|string',
            'applicant.current_position' => 'sometimes|required|string|max:255',
            'applicant.start_work_date' => 'sometimes|required|date',
            'applicant.stock_monthly_contribution' => 'sometimes|required|numeric|min:0',
            'applicant.fee_monthly_contribution' => 'sometimes|required|numeric|min:0',

            'heirs' => 'sometimes|array|min:1',
            'heirs.*.name' => 'required|string|max:255',
            'heirs.*.ic_number' => 'required|string|max:20',
            'heirs.*.relationship' => 'required|string|max:255',
            'heirs.*.phone_number' => 'required|string|max:20',

            'references' => 'sometimes|array',
            'references.proposer_ic_number' => 'nullable|string|max:20',
            'references.supporter_ic_number' => 'nullable|string|max:20',
        ];
    }

    /**
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'applicant.name.required' => 'Nama penuh diperlukan.',
            'applicant.email.required' => 'Emel diperlukan.',
            'applicant.email.email' => 'Emel tidak sah.',
            'applicant.ic_number.required' => 'Nombor kad pengenalan diperlukan.',
            'applicant.birth_date.required' => 'Tarikh lahir diperlukan.',
            'applicant.birth_place.required' => 'Tempat lahir diperlukan.',
            'applicant.gender.required' => 'Jantina diperlukan.',
            'applicant.gender.in' => 'Jantina tidak sah.',
            'applicant.marriage_status.required' => 'Status perkahwinan diperlukan.',
            'applicant.address.required' => 'Alamat diperlukan.',
            'applicant.phone_number.required' => 'Nombor telefon diperlukan.',
            'applicant.postcode.required' => 'Poskod diperlukan.',
            'applicant.employer_name.required' => 'Nama majikan diperlukan.',
            'applicant.employer_address.required' => 'Alamat majikan diperlukan.',
            'applicant.current_position.required' => 'Jawatan semasa diperlukan.',
            'applicant.start_work_date.required' => 'Tarikh mula berkhidmat diperlukan.',
            'applicant.stock_monthly_contribution.required' => 'Caruman saham bulanan diperlukan.',
            'applicant.fee_monthly_contribution.required' => 'Caruman yuran bulanan diperlukan.',

            'heirs.min' => 'Sekurang-kurangnya satu waris diperlukan.',
            'heirs.*.name.required' => 'Nama waris diperlukan.',
            'heirs.*.ic_number.required' => 'Nombor kad pengenalan waris diperlukan.',
            'heirs.*.relationship.required' => 'Hubungan waris diperlukan.',
            'heirs.*.phone_number.required' => 'Nombor telefon waris diperlukan.',

            'references.proposer_ic_number.max' => 'Nombor kad pengenalan pencadang tidak sah.',
            'references.supporter_ic_number.max' => 'Nombor kad pengenalan penyokong tidak sah.',
        ];
    }
}
