<?php

namespace App\Models;

use CodeIgniter\Model;

class LeadScoringRuleModel extends Model
{
    protected $table         = 'lead_scoring_rules';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'name', 'criterion', 'operator', 'value', 'points', 'status',
        'created_by', 'updated_by',
    ];

    protected $validationRules = [
        'name'      => 'required|min_length[2]|max_length[100]',
        'criterion' => 'required|in_list[has_email,has_phone,has_company,source,estimated_value,priority]',
        'operator'  => 'required|in_list[equals,not_equals,greater_than,less_than,is_set]',
        'points'    => 'required|integer',
        'status'    => 'permit_empty|in_list[active,inactive]',
    ];

    public function getActive(): array
    {
        return $this->where('status', 'active')->orderBy('id', 'ASC')->findAll();
    }

    public function scopeForList(?string $search = null, ?string $status = null): self
    {
        if ($search) {
            $this->like('name', $search);
        }

        if ($status) {
            $this->where('status', $status);
        }

        return $this;
    }
}
