<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

// ── DB Config — EDIT THESE ─────────────────────────────────
$db_host = 'localhost';
$db_user = 'redhash_leoexx';       // ← your DB username
$db_pass = '$?P;XUSaG6u%m+3h';           // ← your DB password
$db_name = 'redhash_leoexx';

function jsonOut($status, $message, $extra = []) {
    echo json_encode(array_merge(['status' => $status, 'message' => $message], $extra));
    exit;
}

register_shutdown_function(function() {
    $err = error_get_last();
    if ($err && in_array($err['type'], [E_ERROR, E_PARSE, E_CORE_ERROR])) {
        if (!headers_sent()) header('Content-Type: application/json');
        echo json_encode(['status' => 'error', 'message' => 'Server error: ' . $err['message']]);
    }
});

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    jsonOut('error', 'Method not allowed.');
}

$id = intval($_POST['id'] ?? 0);
if (!$id) jsonOut('error', 'Ticket ID missing.');

$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_error) {
    jsonOut('error', 'DB connection failed: ' . $conn->connect_error);
}
$conn->set_charset('utf8mb4');

$stmt = $conn->prepare("DELETE FROM support_tickets WHERE id = ?");
if (!$stmt) jsonOut('error', 'Prepare failed: ' . $conn->error);

$stmt->bind_param('i', $id);

if ($stmt->execute() && $stmt->affected_rows > 0) {
    jsonOut('success', 'Ticket deleted.');
} else {
    jsonOut('error', 'Delete failed or ticket not found.');
}

$stmt->close();
$conn->close();