<?php

namespace App\Domain\Financial\Models;

use App\Domain\Financial\Enums\AccountType;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class FinancialAccount extends Model
{
    use HasUuid, BelongsToAcademy;

    protected $fillable = [
        'academy_id',
        'parent_id',
        'code',
        'name',
        'name_ar',
        'type',
        'category',
        'is_system',
        'is_active',
        'description',
    ];

    protected function casts(): array
    {
        return [
            'type' => AccountType::class,
            'is_system' => 'boolean',
            'is_active' => 'boolean',
        ];
    }

    public function parent(): BelongsTo
    {
        return $this->belongsTo(self::class, 'parent_id');
    }

    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id');
    }

    public function debitTransactions(): HasMany
    {
        return $this->hasMany(Transaction::class, 'debit_account_id');
    }

    public function creditTransactions(): HasMany
    {
        return $this->hasMany(Transaction::class, 'credit_account_id');
    }
}
