<?php
require_once 'config/database.php';

echo "<h1>🧹 Clean Up Zero Marks</h1>";

try {
    // Delete all results with 0 total marks
    $stmt = $pdo->prepare("DELETE FROM results WHERE marks_obtained = 0.00");
    $result = $stmt->execute();
    $deleted_count = $stmt->rowCount();
    
    echo "<div style='background: #d4edda; padding: 15px; border-radius: 5px;'>";
    echo "<p>✅ Deleted $deleted_count zero marks entries</p>";
    echo "</div>";
    
    // Show remaining results
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM results");
    $remaining_count = $stmt->fetch()['count'];
    
    echo "<p><strong>Remaining results:</strong> $remaining_count</p>";
    
    // Show recent valid results
    $stmt = $pdo->query("SELECT * FROM results WHERE marks_obtained > 0 ORDER BY created_at DESC LIMIT 10");
    $valid_results = $stmt->fetchAll();
    
    if (!empty($valid_results)) {
        echo "<h2>Recent Valid Results:</h2>";
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>Student ID</th><th>Subject ID</th><th>Session</th><th>Exam Type</th><th>Total</th><th>Grade</th></tr>";
        foreach ($valid_results as $result) {
            echo "<tr>";
            echo "<td>{$result['student_id']}</td>";
            echo "<td>{$result['subject_id']}</td>";
            echo "<td>{$result['session']}</td>";
            echo "<td>{$result['exam_type']}</td>";
            echo "<td>{$result['marks_obtained']}</td>";
            echo "<td>{$result['grade']}</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
} catch (Exception $e) {
    echo "<div style='background: #f8d7da; padding: 15px; border-radius: 5px;'>";
    echo "<p>❌ Error: " . $e->getMessage() . "</p>";
    echo "</div>";
}

echo "<p><a href='marks/entry.php'>→ Go to Marks Entry</a></p>";
?>