<?php
namespace App\Models;

use App\Core\Model;

class DailyFloat extends Model
{
    public function getLatest()
    {
        $stmt = $this->query("SELECT * FROM daily_floats ORDER BY record_date DESC LIMIT 1");
        return $stmt->fetch();
    }

    public function getByDate($date)
    {
        $stmt = $this->query("SELECT * FROM daily_floats WHERE record_date = ?", [$date]);
        return $stmt->fetch();
    }

    public function create($data)
    {
        // Calculate total KES equivalent using provided rates
        $total = $data['cash_kes'] 
               + ($data['cash_usd'] * $data['rate_usd']) 
               + ($data['cash_eur'] * $data['rate_eur'])
               + ($data['cash_gbp'] * $data['rate_gbp'])
               + $data['mpesa_float']
               + $data['bank_kes']
               + ($data['bank_usd'] * $data['rate_usd']);

        $insertData = [
            'record_date' => $data['record_date'],
            'cash_kes' => $data['cash_kes'],
            'cash_usd' => $data['cash_usd'],
            'cash_eur' => $data['cash_eur'],
            'cash_gbp' => $data['cash_gbp'],
            'mpesa_float' => $data['mpesa_float'],
            'bank_kes' => $data['bank_kes'],
            'bank_usd' => $data['bank_usd'],
            'total_kes_equivalent' => $total,
            'notes' => $data['notes'] ?? '',
            'recorded_by' => $data['recorded_by']
        ];
        return $this->insert('daily_floats', $insertData);
    }
}