<?php

namespace App\Models;

use CodeIgniter\Model;

class RecruiterInvitationModel extends Model
{
    protected $table         = 'recruiter_invitations';
    protected $allowedFields = ['agency_id', 'invited_by', 'email', 'role_id', 'token', 'status', 'expires_at', 'accepted_at', 'created_at'];
    protected $useTimestamps = false; // created_at set manually below to keep insert explicit

    public function findValidByToken(string $token): ?array
    {
        return $this->where('token', $token)
            ->where('status', 'pending')
            ->where('expires_at >', date('Y-m-d H:i:s'))
            ->first();
    }

    public function forAgency(int $agencyId): array
    {
        return $this->where('agency_id', $agencyId)->orderBy('created_at', 'DESC')->findAll();
    }
}
