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

// Set admin session for testing
$_SESSION['user_id'] = 1;
$_SESSION['user_role'] = 'admin';
$_SESSION['user_name'] = 'Admin';

echo "<h2>ইনভয়েজ ডিবাগ</h2>";

try {
    // Check database connection
    echo "<h3>১. ডাটাবেস কানেকশন</h3>";
    $db_name = $pdo->query("SELECT DATABASE()")->fetchColumn();
    echo "<p>✅ ডাটাবেস: $db_name</p>";
    
    // Check tables
    echo "<h3>২. টেবিল চেক</h3>";
    $tables = ['students', 'classes', 'fees', 'fee_payments'];
    foreach ($tables as $table) {
        try {
            $count = $pdo->query("SELECT COUNT(*) FROM $table")->fetchColumn();
            echo "<p>✅ $table: $count রেকর্ড</p>";
        } catch (Exception $e) {
            echo "<p>❌ $table: " . $e->getMessage() . "</p>";
        }
    }
    
    // Check students with classes
    echo "<h3>৩. শিক্ষার্থী ও ক্লাস</h3>";
    $students_with_class = $pdo->query("
        SELECT COUNT(*) FROM students s 
        JOIN classes c ON s.class_id = c.id 
        WHERE s.status = 'active'
    ")->fetchColumn();
    echo "<p>✅ সক্রিয় শিক্ষার্থী (ক্লাস সহ): $students_with_class</p>";
    
    // Test API endpoints
    echo "<h3>৪. API টেস্ট</h3>";
    
    // Test simple API
    $api_url = "http://localhost/bidda/api/get_students_simple.php?class_id=1";
    echo "<p><strong>API URL:</strong> <a href='$api_url' target='_blank'>$api_url</a></p>";
    
    // Test with cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
    
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($http_code === 200) {
        $data = json_decode($response, true);
        if (is_array($data)) {
            echo "<p>✅ API Response: " . count($data) . " জন শিক্ষার্থী</p>";
            if (!empty($data)) {
                echo "<p>নমুনা: " . $data[0]['first_name'] . " " . $data[0]['last_name'] . "</p>";
            }
        } else {
            echo "<p>❌ API Response: Invalid JSON</p>";
            echo "<pre>" . htmlspecialchars(substr($response, 0, 200)) . "</pre>";
        }
    } else {
        echo "<p>❌ API HTTP Code: $http_code</p>";
        echo "<pre>" . htmlspecialchars(substr($response, 0, 200)) . "</pre>";
    }
    
    // Sample students for testing
    echo "<h3>৫. নমুনা শিক্ষার্থী</h3>";
    $sample_students = $pdo->query("
        SELECT s.id, s.first_name, s.last_name, s.roll_number, 
               c.name as class_name, c.section as class_section
        FROM students s
        JOIN classes c ON s.class_id = c.id
        WHERE s.status = 'active'
        ORDER BY s.id
        LIMIT 5
    ")->fetchAll();
    
    if (!empty($sample_students)) {
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>ID</th><th>নাম</th><th>রোল</th><th>ক্লাস</th><th>টেস্ট লিংক</th></tr>";
        foreach ($sample_students as $student) {
            $test_url = "invoice.php?student_id=" . $student['id'] . "&class_id=" . $student['id'];
            echo "<tr>";
            echo "<td>" . $student['id'] . "</td>";
            echo "<td>" . $student['first_name'] . " " . $student['last_name'] . "</td>";
            echo "<td>" . $student['roll_number'] . "</td>";
            echo "<td>" . $student['class_name'] . " " . $student['class_section'] . "</td>";
            echo "<td><a href='$test_url' target='_blank'>টেস্ট</a></td>";
            echo "</tr>";
        }
        echo "</table>";
    }
    
    echo "<hr>";
    echo "<h3>সমাধান:</h3>";
    echo "<p><a href='invoice.php' style='background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>ইনভয়েজ পেজ চেষ্টা করুন</a></p>";
    echo "<p><a href='../api/get_students_simple.php?class_id=1' target='_blank'>API সরাসরি টেস্ট করুন</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>মূল ত্রুটি: " . $e->getMessage() . "</p>";
}
?>