<?php
require_once __DIR__ . "/../config/db.php";
require_once __DIR__ . "/../lib/auth.php";
require_admin();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) { header("Location: letters.php"); exit; }

$st = $pdo->prepare("SELECT cover FROM letters WHERE id=? LIMIT 1");
$st->execute([$id]);
$row = $st->fetch(PDO::FETCH_ASSOC);

$pdo->prepare("DELETE FROM letters WHERE id=?")->execute([$id]);

// cover faylını da silmək (opsional)
if (!empty($row['cover'])) {
  $path = __DIR__ . "/../uploads/letters/" . $row['cover'];
  if (is_file($path)) @unlink($path);
}

header("Location: letters.php");
exit;