<?php

namespace Modules\User\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AddressRequest 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
    {
        // Get the address ID from the route (with null check)
        $addressId = $this->route() ? $this->route('address') : null;

        // 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 [
                'address_type' => 'nullable|string|max:255',
                'address_line_1' => 'nullable|string|max:255',
                'address_line_2' => 'nullable|string|max:255',
                'city' => 'nullable|string|max:255',
                'state' => 'nullable|string|max:255',
                'postcode' => 'nullable|string|max:255',
                'country' => 'nullable|string|max:255',
            ];
        }

        // Build unique email validation rule
        $addressUniqueRule = $addressId ? 'unique:addresses,address_type,'.$addressId : 'unique:addresses,address_type';

        // If this is an update operation, make email and password optional
        if ($isUpdate) {
            return [
                'address_type' => 'required|string|max:255',
                'address_line_1' => 'required|string|max:255',
                'address_line_2' => 'nullable|string|max:255',
                'city' => 'required|string|max:255',
                'state' => 'required|string|max:255',
                'postcode' => 'required|string|max:255',
                'country' => 'required|string|max:255',
            ];
        }

        // Default rules for create operations
        return [
            'address_type' => 'required|string|max:255',
            'address_line_1' => 'required|string|max:255',
            'address_line_2' => 'nullable|string|max:255',
            'city' => 'required|string|max:255',
            'state' => 'required|string|max:255',
            'postcode' => 'required|string|max:255',
            'country' => 'required|string|max:255',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'address_type.required' => 'Jenis alamat diperlukan.',
            'address_type.max' => 'Jenis alamat tidak boleh melebihi 255 aksara.',
            'address_line_1.max' => 'Alamat 1 tidak boleh melebihi 255 aksara.',
            'address_line_2.max' => 'Alamat 2 tidak boleh melebihi 255 aksara.',
            'city.max' => 'Bandar tidak boleh melebihi 255 aksara.',
            'state.max' => 'Negeri tidak boleh melebihi 255 aksara.',
            'postcode.max' => 'Poskod tidak boleh melebihi 255 aksara.',
            'country.max' => 'Negara tidak boleh melebihi 255 aksara.',
        ];
    }
}
