<?php
session_start();
require_once '../config/database.php';

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h2>Delete Debug Tool</h2>";

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo "<p style='color: red;'>User not logged in</p>";
    exit();
}

echo "<p>User ID: " . $_SESSION['user_id'] . "</p>";
echo "<p>User Role: " . ($_SESSION['user_role'] ?? 'Not set') . "</p>";

// Test database connection
try {
    $pdo->query("SELECT 1");
    echo "<p style='color: green;'>Database connection OK</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>Database connection failed: " . $e->getMessage() . "</p>";
    exit();
}

// List some exams for testing
echo "<h3>Available Exams:</h3>";
$stmt = $pdo->query("SELECT id, name FROM exams ORDER BY id DESC LIMIT 5");
$exams = $stmt->fetchAll();

if (empty($exams)) {
    echo "<p>No exams found</p>";
} else {
    foreach ($exams as $exam) {
        echo "<p>ID: {$exam['id']} - Name: {$exam['name']} ";
        echo "<a href='?test_delete={$exam['id']}' style='color: red;'>[Test Delete]</a></p>";
    }
}

// Test deletion if requested
if (isset($_GET['test_delete'])) {
    $exam_id = (int)$_GET['test_delete'];
    echo "<h3>Testing Deletion of Exam ID: $exam_id</h3>";
    
    try {
        $pdo->beginTransaction();
        
        // Check if exam exists
        $stmt = $pdo->prepare("SELECT id, name FROM exams WHERE id = ?");
        $stmt->execute([$exam_id]);
        $exam = $stmt->fetch();
        
        if (!$exam) {
            echo "<p style='color: red;'>Exam not found</p>";
        } else {
            echo "<p>Found exam: " . htmlspecialchars($exam['name']) . "</p>";
            
            // Count related records
            $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM exam_subjects WHERE exam_id = ?");
            $stmt->execute([$exam_id]);
            $subjects_count = $stmt->fetch()['count'];
            echo "<p>Related exam subjects: $subjects_count</p>";
            
            $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM results WHERE exam_id = ?");
            $stmt->execute([$exam_id]);
            $results_count = $stmt->fetch()['count'];
            echo "<p>Related results: $results_count</p>";
            
            // Try to delete results first
            echo "<p>Attempting to delete results...</p>";
            $stmt = $pdo->prepare("DELETE FROM results WHERE exam_id = ?");
            $result = $stmt->execute([$exam_id]);
            echo "<p>Deleted results: " . $stmt->rowCount() . "</p>";
            
            // Try to delete exam subjects
            echo "<p>Attempting to delete exam subjects...</p>";
            $stmt = $pdo->prepare("DELETE FROM exam_subjects WHERE exam_id = ?");
            $result = $stmt->execute([$exam_id]);
            echo "<p>Deleted exam subjects: " . $stmt->rowCount() . "</p>";
            
            // Try to delete the exam
            echo "<p>Attempting to delete exam...</p>";
            $stmt = $pdo->prepare("DELETE FROM exams WHERE id = ?");
            $result = $stmt->execute([$exam_id]);
            echo "<p>Deleted exam: " . $stmt->rowCount() . "</p>";
            
            $pdo->commit();
            echo "<p style='color: green;'>Successfully deleted exam</p>";
            echo "<p><a href='debug_delete.php'>Refresh</a></p>";
        }
        
    } catch (Exception $e) {
        $pdo->rollBack();
        echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
        echo "<pre>" . $e->getTraceAsString() . "</pre>";
    }
}
?>