<?php

// app/Models/FinancialMovement.php

namespace App\Models;

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

class FinancialMovement extends Model
{
    use HasFactory;

    protected $fillable = [
        'digital_account_id', 'payment_id', 'type', 'amount',
        'balance_before', 'balance_after', 'description',
        'transaction_code', 'bank_reference', 'status', 'reconciled',
        'reconciled_at', 'metadata',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'balance_before' => 'decimal:2',
        'balance_after' => 'decimal:2',
        'reconciled' => 'boolean',
        'reconciled_at' => 'datetime',
        'metadata' => 'array',
    ];

    public function digitalAccount()
    {
        return $this->belongsTo(DigitalAccount::class);
    }

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

    public function scopeCredits($query)
    {
        return $query->where('type', 'credit');
    }

    public function scopeDebits($query)
    {
        return $query->where('type', 'debit');
    }

    public function scopePending($query)
    {
        return $query->where('status', 'pending');
    }

    public function scopeCompleted($query)
    {
        return $query->where('status', 'completed');
    }

    public function scopeReconciled($query)
    {
        return $query->where('reconciled', true);
    }
}
