<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class Transaction extends Model
{
    use HasFactory;

    protected $guarded = [];

    protected $casts = [
        'happened_on' => 'date',
    ];

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

    /**
     * Get the account that owns the Transaction
     */
    public function account(): BelongsTo
    {
        return $this->belongsTo(Account::class);
    }

    public function getAccountTypeAttribute(): string
    {
        return $this->account->type;
    }

    /**
     * Get the ledgers associated with the Transaction
     *
     * @return HasOne
     */
    public function ledgers(): HasMany
    {
        return $this->hasMany(Ledger::class);
    }

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