<?php
# ────────────── lee_gwena ──────────────
# Code by: ashley_gwena
# Created: 2026-03-12
# Notes: Validation rules for updating GL accounts
# ───────────────────────────────────────

namespace App\Http\Requests\ChartOfAccounts;

use Illuminate\Foundation\Http\FormRequest;

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

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

        return [
            'account_code' => ['required', 'regex:/^[0-9]+$/', 'max:50', 'unique:general_ledger_parent_accounts,account_code,' . $accountId . ',id'],
            'account_name' => ['required', 'string', 'max:255'],
            'financial_category' => ['required', 'in:income_statement,balance_sheet'],
            'account_type_id' => ['required', 'integer', 'exists:account_types,id'],
            'is_business' => ['sometimes', 'boolean'],
            'is_trust_account' => ['sometimes', 'boolean'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }

    public function withValidator($validator): void
    {
        $validator->after(function ($validator) {
            $isBusiness = (bool) $this->input('is_business', false);
            $isTrust = (bool) $this->input('is_trust_account', false);
            if ($isBusiness && $isTrust) {
                $validator->errors()->add('is_business', 'Business and Trust cannot both be selected.');
            }
        });
    }

}
