<?php

namespace App\Models;

use CodeIgniter\Model;

class SkillModel extends Model
{
    protected $table         = 'skills';
    protected $allowedFields = ['name', 'category'];
    protected $validationRules = [
        'name' => 'required|max_length[100]|is_unique[skills.name,id,{id}]',
    ];

    public function findOrCreateByName(string $name, ?string $category = null): int
    {
        $existing = $this->where('name', $name)->first();
        if ($existing) {
            return $existing['id'];
        }
        return $this->insert(['name' => $name, 'category' => $category], true);
    }

    public function search(string $query, int $limit = 10): array
    {
        return $this->like('name', $query)->limit($limit)->findAll();
    }
}
