<?php
// Debug script to check attendance data for specific filters
session_start();
require_once 'config/database.php';

// Simulate login for testing
$_SESSION['user_id'] = 1;
$_SESSION['user_role'] = 'admin';
$_SESSION['username'] = 'admin';

echo "<h2>Attendance Data Debug</h2>";

// Test the specific filters from your URL
$report_type = 'monthly';
$month_filter = '2026-01';
$class_id = 14;
$session = '2024-25';

echo "<h3>Testing Filters:</h3>";
echo "<p>Report Type: $report_type</p>";
echo "<p>Month: $month_filter</p>";
echo "<p>Class ID: $class_id</p>";
echo "<p>Session: $session</p>";

try {
    // Check if class exists
    $stmt = $pdo->prepare("SELECT id, name, section FROM classes WHERE id = ?");
    $stmt->execute([$class_id]);
    $class = $stmt->fetch();
    
    if ($class) {
        echo "<p style='color:green'>✓ Class found: " . htmlspecialchars($class['name'] . ' - ' . $class['section']) . "</p>";
    } else {
        echo "<p style='color:red'>✗ Class ID $class_id not found</p>";
    }
    
    // Check if there are students in this class and session
    $stmt = $pdo->prepare("SELECT COUNT(*) as count FROM students WHERE class_id = ? AND session = ?");
    $stmt->execute([$class_id, $session]);
    $student_count = $stmt->fetch()['count'];
    echo "<p>Students in class $class_id with session $session: $student_count</p>";
    
    if ($student_count > 0) {
        // Check attendance records for this month
        $stmt = $pdo->prepare("
            SELECT COUNT(*) as count 
            FROM attendance a 
            INNER JOIN students s ON a.student_id = s.id 
            WHERE DATE_FORMAT(a.date, '%Y-%m') = ? 
            AND s.class_id = ? 
            AND s.session = ?
        ");
        $stmt->execute([$month_filter, $class_id, $session]);
        $attendance_count = $stmt->fetch()['count'];
        echo "<p style='color:" . ($attendance_count > 0 ? "green" : "red") . "'>Attendance records for $month_filter: $attendance_count</p>";
        
        if ($attendance_count > 0) {
            // Show sample attendance records
            $stmt = $pdo->prepare("
                SELECT a.date, a.status, a.remarks, 
                       s.first_name, s.last_name, s.student_id, s.roll_number,
                       c.name as class_name, c.section as class_section
                FROM attendance a 
                INNER JOIN students s ON a.student_id = s.id 
                LEFT JOIN classes c ON s.class_id = c.id
                WHERE DATE_FORMAT(a.date, '%Y-%m') = ? 
                AND s.class_id = ? 
                AND s.session = ?
                ORDER BY a.date, s.roll_number
                LIMIT 10
            ");
            $stmt->execute([$month_filter, $class_id, $session]);
            $records = $stmt->fetchAll();
            
            echo "<h3>Sample Attendance Records:</h3>";
            echo "<table border='1' cellpadding='5'>";
            echo "<tr><th>Date</th><th>Name</th><th>Student ID</th><th>Roll</th><th>Status</th><th>Remarks</th></tr>";
            foreach ($records as $record) {
                echo "<tr>";
                echo "<td>" . $record['date'] . "</td>";
                echo "<td>" . htmlspecialchars($record['first_name'] . ' ' . $record['last_name']) . "</td>";
                echo "<td>" . htmlspecialchars($record['student_id']) . "</td>";
                echo "<td>" . htmlspecialchars($record['roll_number']) . "</td>";
                echo "<td>" . htmlspecialchars($record['status']) . "</td>";
                echo "<td>" . htmlspecialchars($record['remarks']) . "</td>";
                echo "</tr>";
            }
            echo "</table>";
        }
    }
    
    // Also check teacher attendance for the same period
    $stmt = $pdo->prepare("
        SELECT COUNT(*) as count 
        FROM attendance a 
        WHERE DATE_FORMAT(a.date, '%Y-%m') = ? 
        AND a.teacher_id IS NOT NULL
    ");
    $stmt->execute([$month_filter]);
    $teacher_attendance_count = $stmt->fetch()['count'];
    echo "<p>Teacher attendance records for $month_filter: $teacher_attendance_count</p>";
    
} catch (Exception $e) {
    echo "<p style='color:red'>Error: " . $e->getMessage() . "</p>";
}

echo "<h3>Direct Link Test:</h3>";
echo "<p><a href='attendance/comprehensive_report.php?generate=1&report_type=monthly&date=2026-01-16&week=2026-W03&month=2026-01&year=2026&class_id=14&session=2024-25&search=' target='_blank'>Test the report page</a></p>";
?>