<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class SavingsTransaction extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'tenant_id',
        'savings_account_id',
        'customer_id',
        'created_by',
        'approved_by',
        'canceled_by',
        'type',
        'amount',
        'processing_fee_amount',
        'net_amount',
        'balance_before',
        'balance_after',
        'reference',
        'method',
        'status',
        'description',
        'transacted_at',
        'approved_at',
        'canceled_at',
        'canceled_reason',
    ];

    protected function casts(): array
    {
        return [
            'amount' => 'decimal:2',
            'processing_fee_amount' => 'decimal:2',
            'net_amount' => 'decimal:2',
            'balance_before' => 'decimal:2',
            'balance_after' => 'decimal:2',
            'transacted_at' => 'datetime',
            'approved_at' => 'datetime',
            'canceled_at' => 'datetime',
        ];
    }

    public function account(): BelongsTo
    {
        return $this->belongsTo(SavingsAccount::class, 'savings_account_id');
    }

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

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

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

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