<?php

declare(strict_types=1);

namespace App\Models;

use CodeIgniter\Model;

class LeadNoteModel extends Model
{
    protected $table            = 'lead_notes';
    protected $primaryKey       = 'id';
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $allowedFields    = [
        'lead_id',
        'user_id',
        'note_text',
        'created_at',
        'updated_at',
    ];
    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
    {
        $builder = $this->builder();
        $builder->select('lead_notes.*, users.full_name as user_name, users.username as user_username');
        $builder->join('users', 'users.id = lead_notes.user_id', 'left');
        $builder->where('lead_notes.lead_id', $leadId);
        $builder->orderBy('lead_notes.created_at', 'DESC');
        return $builder->get()->getResultArray();
    }
}
