<?php

namespace App\Models;

use CodeIgniter\Model;

class AgencySubscriptionModel extends Model
{
    protected $table         = 'agency_subscriptions';
    protected $useTimestamps = false;
    protected $allowedFields = [
        'agency_id', 'plan_id', 'billing_cycle', 'status',
        'current_period_start', 'current_period_end', 'auto_renew', 'cancelled_at', 'created_at',
    ];

    public function activeFor(int $agencyId): ?array
    {
        return $this->where('agency_id', $agencyId)
            ->whereIn('status', ['trialing', 'active', 'past_due'])
            ->orderBy('created_at', 'DESC')
            ->first();
    }

    public function expiringBefore(string $date): array
    {
        return $this->where('status', 'active')->where('current_period_end <=', $date)->findAll();
    }
}
