<?php
namespace App\Models;

use Core\Model;

class Incident extends Model
{
    protected static string $table = 'incidents';
    protected static string $primaryKey = 'id';
    
    // CRITICAL: Must be true. This is the core of the Admin Analytics and reporting system.
    protected static bool $auditable = true;

    /**
     * Fetch the full details of an incident, joining victim, category, and assigned responder data.
     */
    public static function getDetails(int $id): ?array
    {
        $sql = "
            SELECT 
                i.*, 
                c.name as category_name,
                v.full_name as victim_name,
                v.phone_number as victim_phone,
                r.organization_name as responder_org,
                ru.full_name as responder_name,
                ru.phone_number as responder_phone
            FROM 
                incidents i
            LEFT JOIN categories c ON i.category_id = c.id
            LEFT JOIN users v ON i.victim_id = v.id
            LEFT JOIN responders r ON i.assigned_responder_id = r.id
            LEFT JOIN users ru ON r.user_id = ru.id
            WHERE 
                i.id = ?
        ";

        $result = self::fetchAll($sql, [$id]);
        return $result[0] ?? null;
    }
}