<?php
ob_start();
$response = ['success' => false, 'data' => []];

try {
    require_once 'db_connection.php';
    require_once '../files/guardiankey.php';

    if (isset($_GET['id'])) {
        try {
            $sql = "SELECT message, type, created_at FROM complaint_updates WHERE complaint_id = :id ORDER BY created_at ASC";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([':id' => $_GET['id']]);
            $history = $stmt->fetchAll(PDO::FETCH_ASSOC);
            
            $response['success'] = true;
            $response['data'] = $history;
            
        } catch (PDOException $e) {
            $response['message'] = 'ERROR DE BASE DE DATOS';
        }
    } else {
        $response['message'] = 'ID REQUERIDO';
    }

} catch (Exception $e) {
    $response['message'] = 'ERROR CRITICO';
}

if (ob_get_level() > 0) {
    ob_end_clean(); 
}

header('Content-Type: application/json');
echo json_encode($response);
exit;
?>