<?php

namespace App\Models;

use CodeIgniter\Model;

class SubscriptionUsageModel extends Model
{
    protected $table         = 'subscription_usage';
    protected $useTimestamps = false;
    protected $allowedFields = [
        'agency_subscription_id', 'candidate_views_used', 'contact_reveals_used',
        'resume_downloads_used', 'jobs_posted_used', 'period_start', 'period_end',
    ];

    public function currentFor(int $agencySubscriptionId): ?array
    {
        return $this->where('agency_subscription_id', $agencySubscriptionId)
            ->where('period_end >=', date('Y-m-d'))
            ->orderBy('period_start', 'DESC')
            ->first();
    }

    public function incrementCounter(int $usageId, string $column, int $by = 1): void
    {
        $this->set($column, "{$column} + {$by}", false)->where('id', $usageId)->update();
    }
}
