<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Expense extends Model
{
    protected $fillable = [
        'expense_number','branch_id','expense_category_id','financial_account_id','recurring_expense_id','occurrence_date',
        'description','amount','expense_date','supplier','receipt_original_name','receipt_path','receipt_mime_type','receipt_size',
        'notes','status','entered_by','approved_by','approved_at','rejected_by','rejected_at','decision_comments',
        'financial_account_transaction_id','accounting_journal_number',
    ];

    protected function casts(): array
    {
        return [
            'amount'=>'decimal:2','expense_date'=>'date','occurrence_date'=>'date','approved_at'=>'datetime','rejected_at'=>'datetime',
        ];
    }

    public function branch(): BelongsTo { return $this->belongsTo(Branch::class); }
    public function category(): BelongsTo { return $this->belongsTo(ExpenseCategory::class, 'expense_category_id'); }
    public function financialAccount(): BelongsTo { return $this->belongsTo(FinancialAccount::class); }
    public function recurringExpense(): BelongsTo { return $this->belongsTo(RecurringExpense::class); }
    public function enteredBy(): BelongsTo { return $this->belongsTo(User::class, 'entered_by'); }
    public function approver(): BelongsTo { return $this->belongsTo(User::class, 'approved_by'); }
    public function rejecter(): BelongsTo { return $this->belongsTo(User::class, 'rejected_by'); }
    public function accountTransaction(): BelongsTo { return $this->belongsTo(FinancialAccountTransaction::class, 'financial_account_transaction_id'); }
}