<?php

namespace App\Models;

use CodeIgniter\Model;

class CreditTransactionModel extends Model
{
    protected $table         = 'credit_transactions';
    protected $useTimestamps = false;
    protected $allowedFields = ['agency_id', 'user_id', 'type', 'amount', 'balance_after', 'reference_type', 'reference_id', 'expires_at', 'created_at'];

    public function historyFor(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);
    }

    public function expiringUnexpired(): array
    {
        return $this->where('type', 'bonus')->where('expires_at <=', date('Y-m-d'))->where('expires_at IS NOT NULL')->findAll();
    }
}
