<?php

namespace Modules\User\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class HeirRequest 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
    {
        // Check if this is a create or update operation
        $isCreate = $this->isMethod('POST');
        $isUpdate = $this->isMethod('PUT') || $this->isMethod('PATCH');

        // If this is not a POST, PUT, or PATCH request, return minimal rules
        if (! $isCreate && ! $isUpdate) {
            return [
                'name' => 'nullable|string|max:255',
                'ic_number' => 'nullable|string|max:255',
                'relationship' => 'nullable|string|max:255',
                'phone_number' => 'nullable|string|max:255',
                'address' => 'nullable|string',
                'is_primary' => 'nullable|boolean',
            ];
        }

        // If this is an update operation, make email and password optional
        if ($isUpdate) {
            return [
                'name' => 'required|string|max:255',
                'ic_number' => 'required|string|max:255',
                'relationship' => 'required|string|max:255',
                'phone_number' => 'required|string|max:255',
                'address' => 'required|string',
                'is_primary' => 'required|boolean',
            ];
        }

        // Default rules for create operations
        return [
            'name' => 'required|string|max:255',
            'ic_number' => 'required|string|max:255',
            'relationship' => 'required|string|max:255',
            'phone_number' => 'required|string|max:255',
            'address' => 'required|string',
            'is_primary' => 'required|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Nama diperlukan.',
            'name.max' => 'Nama tidak boleh melebihi 255 aksara.',
            'ic_number.required' => 'Nombor Kad Pengenalan diperlukan.',
            'ic_number.max' => 'Nombor Kad Pengenalan tidak boleh melebihi 255 aksara.',
            'relationship.required' => 'Hubungan diperlukan.',
            'relationship.max' => 'Hubungan tidak boleh melebihi 255 aksara.',
            'phone_number.required' => 'Nombor telefon diperlukan.',
            'phone_number.max' => 'Nombor telefon tidak boleh melebihi 255 aksara.',
            'address.required' => 'Alamat diperlukan.',
            'address.string' => 'Alamat tidak sah.',
            'is_primary.required' => 'Status pewaris utama diperlukan.',
            'is_primary.boolean' => 'Status pewaris utama tidak sah.',
        ];
    }
}
