<?php
require_once __DIR__ . '/../../config/config.php';
header('Content-Type: application/json');

try {
    $announcement_id = (int)($_POST['announcement_id'] ?? $_GET['id'] ?? 0);
    
    if ($announcement_id <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'Invalid announcement ID']);
        exit;
    }
    
    global $pdo;
    
    // Check if table exists
    $stmt = $pdo->query("SHOW TABLES LIKE 'result_announcements'");
    if ($stmt->rowCount() == 0) {
        echo json_encode(['status' => 'error', 'message' => 'Table not found']);
        exit;
    }
    
    // Check if record exists
    $stmt = $pdo->prepare("SELECT announcement_id FROM result_announcements WHERE announcement_id = ?");
    $stmt->execute([$announcement_id]);
    if ($stmt->rowCount() == 0) {
        echo json_encode(['status' => 'error', 'message' => 'Announcement not found']);
        exit;
    }
    
    // Delete the record
    $stmt = $pdo->prepare("DELETE FROM result_announcements WHERE announcement_id = ?");
    $stmt->execute([$announcement_id]);
    
    if ($stmt->rowCount() > 0) {
        echo json_encode(['status' => 'success', 'message' => 'Announcement deleted successfully']);
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Failed to delete announcement']);
    }
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>
