<?php

declare(strict_types=1);

namespace App\Models;

class LeadStatusHistory extends BaseModel
{
    protected string $table = 'lead_status_history';
    protected string $primaryKey = 'id';

    public function recordChange(int $leadId, ?string $previousStatus, string $newStatus, ?int $lossReasonId, ?string $notes, int $changedBy): int
    {
        return $this->create([
            'lead_id' => $leadId,
            'previous_status' => $previousStatus,
            'new_status' => $newStatus,
            'loss_reason_id' => $lossReasonId,
            'notes' => $notes,
            'changed_by' => $changedBy,
        ]);
    }

    public function getByLeadId(int $leadId): array
    {
        return $this->query(
            "SELECT h.*, u.name as changed_by_name
             FROM {$this->table} h
             LEFT JOIN users u ON h.changed_by = u.id
             WHERE h.lead_id = :lead_id
             ORDER BY h.created_at DESC
             LIMIT 100",
            ['lead_id' => $leadId]
        );
    }
}
