<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}

require_once '../src/config/db.php';

if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('Location: certifications.php');
    exit;
}

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

try {
    $stmt = $pdo->prepare("SELECT image_url FROM certifications WHERE id = ?");
    $stmt->execute([$id]);
    $cert = $stmt->fetch();

    if ($cert && !empty($cert['image_url'])) {
        $imagePath = '../public/' . $cert['image_url'];
        if (file_exists($imagePath)) {
            unlink($imagePath);
        }
    }

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

    header('Location: certifications.php?success=delete');
    exit;
} catch (PDOException $e) {
    error_log("Error deleting certification: " . $e->getMessage());
    header('Location: certifications.php?error=delete_failed');
    exit;
}