<?php

namespace App\Models;

use CodeIgniter\Model;

class PaymentModel extends Model
{
    protected $table         = 'payments';
    protected $useTimestamps = false;
    protected $allowedFields = ['agency_id', 'invoice_id', 'gateway', 'gateway_transaction_id', 'purpose', 'amount', 'currency', 'status', 'gateway_response', 'created_at'];
    protected $casts         = ['gateway_response' => 'json'];

    public function findByGatewayTransactionId(string $gateway, string $transactionId): ?array
    {
        return $this->where('gateway', $gateway)->where('gateway_transaction_id', $transactionId)->first();
    }

    public function forAgency(int $agencyId, int $page = 1, int $perPage = 20): array
    {
        $offset = ($page - 1) * $perPage;
        return $this->where('agency_id', $agencyId)->orderBy('created_at', 'DESC')->findAll($perPage, $offset);
    }
}
