<?php

namespace App\Models;

use CodeIgniter\Model;

class ExchangeRateModel extends Model
{
    protected $table = 'exchange_rates';
    protected $primaryKey = 'id';
    protected $useTimestamps = true;
    protected $allowedFields = ['currency_from', 'currency_to', 'rate', 'date'];

    public function latestRate(string $from, string $to): ?float
    {
        if ($from === $to) return 1.0;

        $row = $this->where('currency_from', $from)
            ->where('currency_to', $to)
            ->orderBy('date', 'DESC')
            ->first();

        return $row ? (float) $row['rate'] : null;
    }
}
