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

// Let's check a specific student's data
echo "Checking student payment data...\n";

// First, let's get a student who has fees
$stmt = $pdo->query("SELECT s.id as student_id, s.student_id as student_code, s.first_name, s.last_name, COUNT(f.id) as fee_count FROM students s JOIN fees f ON s.id = f.student_id GROUP BY s.id, s.student_id, s.first_name, s.last_name HAVING COUNT(f.id) > 0 ORDER BY s.id LIMIT 1");
$student = $stmt->fetch();

if ($student) {
    echo "Testing student: " . $student['first_name'] . " " . $student['last_name'] . " (ID: " . $student['student_code'] . ", DB ID: " . $student['student_id'] . ")\n";
    echo "Number of fees: " . $student['fee_count'] . "\n";
    
    // Now check if this student has any payments
    $stmt = $pdo->prepare("SELECT fp.*, f.fee_type, f.month FROM fee_payments fp JOIN fees f ON f.id = fp.fee_id WHERE f.student_id = ?");
    $stmt->execute([$student['student_id']]);
    $payments = $stmt->fetchAll();
    
    echo "Number of payments found: " . count($payments) . "\n";
    
    if (count($payments) > 0) {
        echo "Payments:\n";
        foreach ($payments as $payment) {
            echo "- Amount: " . $payment['amount'] . ", Method: " . $payment['payment_method'] . ", Fee: " . $payment['fee_type'] . " " . $payment['month'] . "\n";
        }
    } else {
        echo "No payments found for this student.\n";
        
        // Let's check if there are any payments at all in the system for any student
        $stmt = $pdo->query("SELECT fp.*, f.fee_type, f.month, f.student_id FROM fee_payments fp JOIN fees f ON f.id = fp.fee_id LIMIT 5");
        $allPayments = $stmt->fetchAll();
        
        echo "Sample of all payments in system:\n";
        foreach ($allPayments as $payment) {
            echo "- Student ID: " . $payment['student_id'] . ", Amount: " . $payment['amount'] . ", Fee: " . $payment['fee_type'] . " " . $payment['month'] . "\n";
        }
    }
} else {
    echo "No students with fees found.\n";
}
?>