<?php
/**
 * Delete Rubric
 * Delete a rubric and all its children
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';
require_once '../../includes/rubric-tree.php';

requireAdmin();

$pdo = getDB();
$rubricId = (int)($_GET['id'] ?? 0);
$error = '';
$success = '';

// Get rubric details
$stmt = $pdo->prepare("SELECT * FROM rubrics WHERE id = ?");
$stmt->execute([$rubricId]);
$rubric = $stmt->fetch();

if (!$rubric) {
    header('Location: index.php?error=Rubric not found');
    exit;
}

// Count children
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM rubrics WHERE parent_id = ?");
$stmt->execute([$rubricId]);
$childCount = $stmt->fetch()['count'];

// Count remedies
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM rubric_remedy WHERE rubric_id = ?");
$stmt->execute([$rubricId]);
$remedyCount = $stmt->fetch()['count'];

// Handle deletion
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $confirm = $_POST['confirm'] ?? '';
    
    if ($confirm == 'DELETE') {
        beginTransaction();
        
        try {
            // Delete rubric (cascade will delete children and rubric_remedy)
            $stmt = $pdo->prepare("DELETE FROM rubrics WHERE id = ?");
            $stmt->execute([$rubricId]);
            
            commitTransaction();
            $success = "Rubric deleted successfully!";
            header("refresh:2;url=index.php");
            
        } catch (Exception $e) {
            rollbackTransaction();
            $error = "Failed to delete: " . $e->getMessage();
        }
    } else {
        $error = "Please type DELETE to confirm deletion";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete Rubric - Admin Panel</title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-6 mx-auto">
                <div class="card">
                    <div class="card-header bg-danger text-white">
                        <h4 class="mb-0"><i class="fas fa-exclamation-triangle"></i> Delete Rubric</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?php echo $error; ?></div>
                        <?php endif; ?>
                        
                        <?php if ($success): ?>
                            <div class="alert alert-success"><?php echo $success; ?></div>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                <strong>⚠️ Warning!</strong><br>
                                You are about to delete the following rubric:
                            </div>
                            
                            <table class="table table-bordered">
                                <tr><th>Rubric:</th><td><strong><?php echo escape($rubric['rubric_name']); ?></strong>