<?php
namespace App\Models;

use Core\Model;

class Message extends Model
{
    protected static string $table = 'messages';
    protected static string $primaryKey = 'id';
    
    // We typically don't want to audit every single chat message in the audit_logs table 
    // as it will quickly bloat the database. The messages table itself serves as the transcript.
    protected static bool $auditable = false;

    /**
     * Fetch all messages for a specific incident, including sender details.
     */
    public static function getByIncident(int $incidentId): array
    {
        $sql = "
            SELECT 
                m.*, 
                u.full_name as sender_name,
                u.role as sender_role
            FROM 
                messages m
            JOIN 
                users u ON m.sender_id = u.id
            WHERE 
                m.incident_id = ?
            ORDER BY 
                m.created_at ASC
        ";

        return self::fetchAll($sql, [$incidentId]);
    }
}