<?php

namespace App\Domain\Financial\Models;

use App\Domain\Financial\Enums\TransactionType;
use App\Domain\Shared\Traits\Auditable;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Transaction extends Model
{
    use HasUuid, BelongsToAcademy, Auditable;

    const UPDATED_AT = null;

    protected $fillable = [
        'academy_id',
        'debit_account_id',
        'credit_account_id',
        'payment_id',
        'invoice_id',
        'reference_type',
        'reference_id',
        'amount',
        'currency',
        'type',
        'description',
        'transaction_date',
        'metadata',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'type' => TransactionType::class,
            'amount' => 'integer',
            'transaction_date' => 'date',
            'metadata' => 'array',
        ];
    }

    public function debitAccount(): BelongsTo
    {
        return $this->belongsTo(FinancialAccount::class, 'debit_account_id');
    }

    public function creditAccount(): BelongsTo
    {
        return $this->belongsTo(FinancialAccount::class, 'credit_account_id');
    }

    public function payment(): BelongsTo
    {
        return $this->belongsTo(Payment::class);
    }

    public function invoice(): BelongsTo
    {
        return $this->belongsTo(Invoice::class);
    }

    public function reference(): MorphTo
    {
        return $this->morphTo();
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

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