<?php
session_start();
require_once "includes/config.php";

// Check if user is logged in
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}

// Only allow POST requests
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo "error: invalid request method";
    exit;
}

// Validate ID
if(!isset($_POST['id']) || !is_numeric($_POST['id'])) {
    echo "error: invalid contact ID";
    exit;
}

$id = (int)$_POST['id'];

// Delete the contact submission
$stmt = $conn->prepare("DELETE FROM contact_submissions WHERE id = ?");
$stmt->bind_param("i", $id);

if($stmt->execute()){
    $stmt->close();
    header("Location: contacts.php?deleted=1");
    exit;
} else {
    echo "error: could not delete submission";
}
?>
