<?php

namespace App\Models;

use CodeIgniter\Model;

class PaymentModel extends Model
{
    protected $table = 'payments';
    protected $primaryKey = 'id';
    protected $allowedFields = [
        'invoice_id', 'amount', 'payment_date', 'payment_method', 'reference', 'notes', 'created_by'
    ];
    protected $returnType = 'array';
    protected $useTimestamps = true;

    public function getTotalPaid(int $invoiceId): float
    {
        $result = $this->where('invoice_id', $invoiceId)->selectSum('amount')->first();
        return (float) ($result['amount'] ?? 0);
    }
}
