<?php
/**
 * SAC 2025 - DELETE MAINTENANCE
 * Limpieza física de tickets de falla del historial.
 */
require_once 'config.php';

checkAccess(['admin', 'programmer', 'board']);

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

if (!isset($data['id'])) {
    response(["error" => "ID de ticket no proporcionado"], 400);
}

try {
    $stmtInfo = $pdo->prepare("SELECT type, description FROM maintenance_requests WHERE id = ?");
    $stmtInfo->execute([$data['id']]);
    $maint = $stmtInfo->fetch();

    if ($maint) {
        $stmt = $pdo->prepare("DELETE FROM maintenance_requests WHERE id = ?");
        $stmt->execute([$data['id']]);

        logSystemAction("BORRÓ TICKET DE FALLA: " . $maint['type'] . " - " . substr($maint['description'], 0, 50), "SERVICIOS");
        response(["success" => true]);
    } else {
        response(["error" => "Ticket no encontrado"], 404);
    }
} catch (PDOException $e) {
    response(["error" => "Error al eliminar ticket: " . $e->getMessage()], 500);
}
?>