<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Model;

class LeadCustomValueModel extends Model
{
    protected $table            = 'lead_custom_values';
    protected $primaryKey       = 'id';
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'lead_id',
        'field_id',
        'field_value',
    ];
    protected $useTimestamps    = true;
    protected $createdField     = 'created_at';
    protected $updatedField     = 'updated_at';
    protected $deletedField     = 'deleted_at';
    protected $validationRules  = [];
    protected $validationMessages = [];
    protected $skipValidation     = false;

    public function getByLeadId(int $leadId): array
    {
        $rows = $this->where('lead_id', $leadId)->findAll();
        $result = [];
        foreach ($rows as $row) {
            $result[$row['field_id']] = $row;
        }
        return $result;
    }
}
