<?php

namespace App\Models;

use App\Models\Scopes\BranchScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class AccountingEntry extends Model
{
    public const CATEGORIES = [
        'assets','capital','principal','fixed_assets','accumulated_depreciation',
        'interest','fees','penalties','service_income','other_income',
        'expenses','payroll','depreciation_expense','asset_maintenance',
        'write_offs','write_off_recoveries','settlement_adjustment','other_expenses',
    ];

    protected $fillable = ['journal_number','branch_id','financial_account_id','entry_date','reference','category','event_type','description','debit','credit','user_id','related_type','related_id'];

    protected function casts(): array
    {
        return ['entry_date'=>'datetime','debit'=>'decimal:2','credit'=>'decimal:2'];
    }

    protected static function booted(): void
    {
        static::addGlobalScope(new BranchScope());
    }

    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function financialAccount(): BelongsTo { return $this->belongsTo(FinancialAccount::class); }
    public function user(): BelongsTo { return $this->belongsTo(User::class); }
    public function related(): MorphTo { return $this->morphTo(); }
}
