<?php

namespace App\Models;

use CodeIgniter\Model;

class FootballCountryModel extends Model
{
    protected $table = 'football_countries';
    protected $primaryKey = 'id';
    protected $allowedFields = ['api_name', 'code', 'flag_url', 'created_at', 'updated_at'];
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';

    public function search(?string $q = null, int $limit = 500): array
    {
        $builder = $this->orderBy('api_name', 'ASC');

        if ($q !== null && strlen(trim($q)) >= 2) {
            $builder->like('api_name', trim($q));
        }

        return $builder->findAll($limit);
    }

    public function findByApiName(string $apiName): ?array
    {
        return $this->where('api_name', $apiName)->first();
    }
}
