<?php
declare(strict_types=1);
require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/helpers.php';
require_admin();

if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !verify_csrf($_POST['csrf'] ?? null)) {
    header('Location: dashboard.php');
    exit;
}

$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($id) {
    try {
        $stmt = db()->prepare('DELETE FROM posts WHERE id = :id');
        $stmt->execute(['id' => $id]);
    } catch (Throwable $e) {
        // Fail safely: if DB is unavailable, just redirect back.
    }
}

header('Location: dashboard.php');
exit;
