<?php
require_once '../../includes/db.php';

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

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

// Fetch photo filename to delete image
$stmt = $pdo->prepare("SELECT photo FROM celebrities WHERE id = ?");
$stmt->execute([$id]);
$celeb = $stmt->fetch();

if ($celeb) {
    $photo_path = '../images/' . $celeb['photo'];
    if (file_exists($photo_path)) {
        unlink($photo_path);
    }

    $del_stmt = $pdo->prepare("DELETE FROM celebrities WHERE id = ?");
    $del_stmt->execute([$id]);
    header("Location: list_celebrity.php?msg=deleted");
    exit;
} else {
    header("Location: list_celebrity.php?msg=error");
    exit;
}
