<?php
// Test script to verify assignment database functionality
require_once 'config/database.php';

echo "=== Testing Assignment Database Functionality ===\n\n";

try {
    // Test fetching classes
    echo "1. Testing class fetching...\n";
    $stmt = $pdo->query("SELECT id, name, section FROM classes WHERE status = 'active' ORDER BY name LIMIT 5");
    $classes = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($classes) > 0) {
        echo "   ✓ Successfully fetched " . count($classes) . " classes\n";
        foreach ($classes as $class) {
            echo "     - ID: " . $class['id'] . ", Name: " . $class['name'] . ", Section: " . $class['section'] . "\n";
        }
    } else {
        echo "   ! No classes found\n";
    }
    
    echo "\n2. Testing subject fetching...\n";
    $stmt = $pdo->query("SELECT id, name FROM subjects WHERE status = 'active' ORDER BY name LIMIT 5");
    $subjects = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($subjects) > 0) {
        echo "   ✓ Successfully fetched " . count($subjects) . " subjects\n";
        foreach ($subjects as $subject) {
            echo "     - ID: " . $subject['id'] . ", Name: " . $subject['name'] . "\n";
        }
    } else {
        echo "   ! No subjects found\n";
    }
    
    echo "\n3. Testing assignment fetching...\n";
    $stmt = $pdo->query("SELECT a.*, c.name as class_name, s.name as subject_name FROM assignments a JOIN classes c ON a.class_id = c.id JOIN subjects s ON a.subject_id = s.id ORDER BY a.created_at DESC LIMIT 3");
    $assignments = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    if (count($assignments) > 0) {
        echo "   ✓ Successfully fetched " . count($assignments) . " assignments\n";
        foreach ($assignments as $assignment) {
            echo "     - ID: " . $assignment['id'] . ", Title: " . $assignment['title'] . ", Class: " . $assignment['class_name'] . ", Subject: " . $assignment['subject_name'] . "\n";
        }
    } else {
        echo "   ! No assignments found\n";
    }
    
    echo "\n✓ Assignment database functionality test completed\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>