<?php
session_start();
if (!isset($_SESSION['admin'])) {
    header("Location: index.php");
    exit;
}

if (isset($_GET['id'])) {
    $file = '../data/messages.json';
    $messages = file_exists($file) ? json_decode(file_get_contents($file), true) : [];

    $id = (int) $_GET['id'];
    if (isset($messages[$id])) {
        array_splice($messages, $id, 1); // Remove the message at index
        file_put_contents($file, json_encode($messages, JSON_PRETTY_PRINT));
    }
}

header("Location: dashboard.php#messages");
exit;
