<?php
$db = getDB();
$type = $_POST['type'] ?? '';
$id = (int)($_POST['delete_id'] ?? 0);
$token = $_POST['csrf_token'] ?? '';

if (!verifyCsrfToken($token)) {
    header("Location: /admin");
    exit;
}

$tableMap = [
    'sliders' => 'sliders',
    'sections' => 'sections',
    'announcements' => 'announcements',
    'resources' => 'resources',
    'facilities' => 'facilities',
    'services' => 'services',
    'workshops' => 'workshops',
    'articles' => 'articles',
    'article_categories' => 'article_categories',
    'team_members' => 'team_members',
    'alumni_team' => 'alumni_team',
    'seo_metadata' => 'seo_metadata',
];

$redirectMap = [
    'sliders' => '/admin/sliders',
    'sections' => '/admin/sections',
    'announcements' => '/admin/announcements',
    'resources' => '/admin/resources',
    'facilities' => '/admin/facilities',
    'services' => '/admin/services',
    'workshops' => '/admin/workshops',
    'articles' => '/admin/articles',
    'article_categories' => '/admin/categories',
    'team_members' => '/admin/team',
    'alumni_team' => '/admin/alumni',
    'seo_metadata' => '/admin/seo',
];

if (isset($tableMap[$type]) && $id > 0) {
    $tableName = $tableMap[$type];
    $stmt = $db->prepare("DELETE FROM " . $tableName . " WHERE id = ?");
    $stmt->execute([$id]);
    logAudit('delete', $type, $id, "Deleted $type #$id");
}

$redirect = $redirectMap[$type] ?? '/admin';
header("Location: $redirect");
exit;
