<?php

header("Content-Type: application/json");
require("../includes/config.php");

$data = json_decode(file_get_contents("php://input"), true);
$id = $data['id'] ?? 0;

// 🔍 check subjects exist
$check = $pdo->prepare("SELECT COUNT(*) FROM subjects WHERE class_id = ?");
$check->execute([$id]);

if ($check->fetchColumn() > 0) {
    echo json_encode([
        "status" => "error",
        "message" => "Cannot delete class. Subjects exist."
    ]);
    exit;
}

// ✅ safe to delete
$stmt = $pdo->prepare("DELETE FROM classes WHERE id = ?");
$stmt->execute([$id]);

echo json_encode(["status"=>"success","message"=>"Class deleted"]);


?>
