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

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

    $data = json_decode(file_get_contents('php://input'), true);

    if (isset($data['id'])) {
        try {
            $sql = "DELETE FROM complaints WHERE id = :id";
            $stmt = $pdo->prepare($sql);
            $stmt->execute([':id' => $data['id']]);

            if ($stmt->rowCount()) {
                $response['success'] = true;
                $response['message'] = 'QUEJA ELIMINADA';
            } else {
                $response['message'] = 'NO ENCONTRADO';
            }

        } 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;
?>