<?php

namespace App\Models;

use CodeIgniter\Model;

class LeadConversionModel extends Model
{
    protected $table         = 'lead_conversions';
    protected $primaryKey    = 'id';
    protected $returnType    = 'array';
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';

    protected $allowedFields = [
        'lead_id', 'converted_by', 'conversion_type', 'notes', 'converted_at', 'created_at',
    ];

    protected $validationRules = [
        'lead_id'         => 'required|integer',
        'conversion_type' => 'required|in_list[customer,opportunity]',
    ];

    public function scopeForList(?string $search = null, ?string $type = null): self
    {
        if ($type) {
            $this->where('conversion_type', $type);
        }

        return $this;
    }

    public function findByLeadId(int $leadId): ?array
    {
        return $this->where('lead_id', $leadId)->first();
    }
}
