<?php

namespace Modules\Accounts\Http\Requests\BankAccount;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Core\Traits\FailedValidation;

class UpdateBankAccountRequest extends FormRequest
{
    use FailedValidation;

    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $bankAccountId = $this->route('bankAccount')?->id ?? $this->route('bankAccount');

        return [
            'account_id' => ['sometimes', 'integer', 'exists:chart_of_accounts,id', Rule::unique('bank_accounts', 'account_id')->ignore($bankAccountId)],
            'project_id' => ['sometimes', 'integer', 'exists:projects,id'],
            'bank_name' => ['sometimes', 'nullable', 'string', 'max:255'],
            'account_number' => ['sometimes', 'string', 'max:255', Rule::unique('bank_accounts', 'account_number')->ignore($bankAccountId)],
            'branch_name' => ['sometimes', 'nullable', 'string', 'max:255'],
            'routing_no' => ['sometimes', 'nullable', 'string', 'max:255'],
            'currency' => ['sometimes', 'string', 'max:10'],
            'balance' => ['sometimes', 'numeric', 'min:0'],
        ];
    }
}
