<?php
session_start();

require_once '../../config/database.php';
require_once '../../includes/auth.php';

$db = (new Database())->connect();

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die('Invalid category ID.');
}

/*
    Prevent delete if category used in items
*/
$stmt = $db->prepare("
    SELECT COUNT(*)
    FROM items
    WHERE category_id = ?
");

$stmt->execute([$id]);

if ($stmt->fetchColumn() > 0) {
    die('Cannot delete category. Items are linked to this category.');
}

/*
    Delete category
*/
$stmt = $db->prepare("
    DELETE FROM categories
    WHERE id = ?
");

$stmt->execute([$id]);

header('Location: index.php');
exit;
?>