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

namespace App\Http\Requests\ChartOfAccounts;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $subaccountId = $this->route('subaccount')?->id ?? $this->route('subaccount');
        $parentId = $this->input('parent_id') ?? $this->route('parent')?->id;

        return [
            'parent_id' => ['required', 'integer', 'exists:general_ledger_parent_accounts,id'],
            'sub_code' => [
                'required',
                'regex:/^[0-9]+$/',
                'max:20',
                Rule::unique('general_ledger_subaccounts', 'sub_code')
                    ->where(fn ($q) => $q->where('parent_id', $parentId))
                    ->ignore($subaccountId),
            ],
            'account_name' => ['required', 'string', 'max:255'],
            'is_active' => ['sometimes', 'boolean'],
        ];
    }
}
