<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Credit extends Model
{
    use HasFactory;

    protected $fillable = [
        'amount',
        'type',
        'deposit_type',
        'withdrawal_type',
        'status',
        'active',
        'branch_id',
        'user_id',
        'shareholder_id',
        'attachment',
        'notes',
        'created_by',
        'updated_by',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'active' => 'boolean',
        'created_by' => 'integer',
        'updated_by' => 'integer',
    ];

    /**
     * Get the branch that owns the credit.
     */
    public function branch()
    {
        return $this->belongsTo(Branch::class);
    }

    /**
     * Get the user that owns the credit.
     */
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the shareholder user that approved the credit.
     */
    public function shareholder()
    {
        return $this->belongsTo(User::class, 'shareholder_id');
    }

    /**
     * Get the withdrawal approvals for this credit.
     */
    public function withdrawalApprovals()
    {
        return $this->hasMany(WithdrawalApproval::class);
    }
}
