<?php

namespace MoneyTransfer\Core\Models;

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

class ExchangeRate extends Model
{
    use HasFactory;

    protected $fillable = [
        'from_currency',
        'to_currency',
        'rate',
        'inverse_rate',
        'last_updated',
        'source',
        'is_active',
    ];

    protected $casts = [
        'rate' => 'decimal:6',
        'inverse_rate' => 'decimal:6',
        'last_updated' => 'datetime',
        'is_active' => 'boolean',
    ];

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

    public function scopeByCurrencyPair($query, $fromCurrency, $toCurrency)
    {
        return $query->where('from_currency', $fromCurrency)
                    ->where('to_currency', $toCurrency);
    }

    public function scopeRecent($query, $hours = 24)
    {
        return $query->where('last_updated', '>=', now()->subHours($hours));
    }
} 