<?php
// Direct test of the search API
session_start();
require_once '../config/database.php';

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

// Test search functionality
$test_queries = [
    ['search_text' => 'রহিম', 'search_by' => 'name'],
    ['search_text' => '101', 'search_by' => 'roll'],
    ['search_text' => 'STU001', 'search_by' => 'student_id'],
    ['search_text' => '01712345678', 'search_by' => 'phone']
];

echo "<h2>Search API Debug Test</h2>";

foreach ($test_queries as $query) {
    echo "<h3>Testing: {$query['search_text']} ({$query['search_by']})</h3>";
    
    // Simulate the API call
    $_GET = $query;
    
    ob_start();
    include '../api/get_students_simple.php';
    $result = ob_get_clean();
    
    echo "<pre>";
    echo "Raw Response: " . htmlspecialchars($result) . "\n";
    
    $decoded = json_decode($result, true);
    if ($decoded) {
        echo "Decoded JSON: " . print_r($decoded, true);
        if (is_array($decoded)) {
            echo "Found " . count($decoded) . " students\n";
        }
    } else {
        echo "JSON decode error: " . json_last_error_msg() . "\n";
    }
    echo "</pre>";
    echo "<hr>";
}

// Check database structure
echo "<h3>Database Structure Check</h3>";
try {
    $stmt = $pdo->query("DESCRIBE students");
    $columns = $stmt->fetchAll();
    echo "<pre>";
    echo "Students table columns:\n";
    foreach ($columns as $col) {
        echo "- {$col['Field']} ({$col['Type']})\n";
    }
    echo "</pre>";
    
    // Check sample data
    $stmt = $pdo->query("SELECT * FROM students WHERE status = 'active' LIMIT 3");
    $samples = $stmt->fetchAll();
    echo "<pre>";
    echo "Sample data:\n";
    print_r($samples);
    echo "</pre>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Database error: " . $e->getMessage() . "</p>";
}
?>