<?php

namespace Modules\User\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BankRequest 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',
                'code' => 'nullable|string|max:255',
                'swift_code' => 'nullable|string|max:255',
                'is_active' => 'nullable|boolean',
            ];
        }

        // If this is an update operation, make email and password optional
        if ($isUpdate) {
            return [
                'name' => 'required|string|max:255',
                'code' => 'required|string|max:255',
                'swift_code' => 'nullable|string|max:255',
                'is_active' => 'required|boolean',
            ];
        }

        // Default rules for create operations
        return [
            'name' => 'required|string|max:255',
            'code' => 'required|string|max:255',
            'swift_code' => 'nullable|string|max:255',
            'is_active' => 'required|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Nama bank diperlukan.',
            'name.max' => 'Nama bank tidak boleh melebihi 255 aksara.',
            'code.required' => 'Kod bank diperlukan.',
            'code.max' => 'Kod bank tidak boleh melebihi 255 aksara.',
            'swift_code.max' => 'Swift code tidak boleh melebihi 255 aksara.',
            'is_active.required' => 'Status aktif diperlukan.',
            'is_active.boolean' => 'Status aktif tidak sah.',
        ];
    }
}
