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

// Set session variables for testing if not logged in
if (!isset($_SESSION['user_id'])) {
    $_SESSION['user_id'] = 1;
    $_SESSION['user_role'] = 'admin';
    $_SESSION['user_name'] = 'Admin User';
}

echo "<h2>Create Sample Assignment</h2>";

try {
    // Check if we have any assignments
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM assignments");
    $count = $stmt->fetch();
    
    if ($count['count'] == 0) {
        echo "<p>No assignments found. Creating a sample assignment...</p>";
        
        // Check if we have any teachers
        $stmt = $pdo->query("SELECT id FROM teachers LIMIT 1");
        $teacher = $stmt->fetch();
        
        if (!$teacher) {
            echo "<p>No teachers found. You may need to create a teacher first.</p>";
        } else {
            // Insert sample assignment
            $stmt = $pdo->prepare("
                INSERT INTO assignments (title, description, class_id, subject_id, teacher_id, due_date, max_marks, status) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?)
            ");
            
            $result = $stmt->execute([
                'গণিত - বীজগণিত', // title
                'অধ্যায় ৩ - বহুপদী সমীকরণ', // description
                11, // class_id (একাদশ)
                3, // subject_id (গণিত)
                $teacher['id'], // teacher_id
                '2025-10-25 23:59:59', // due_date
                20, // max_marks
                'published' // status
            ]);
            
            if ($result) {
                $assignmentId = $pdo->lastInsertId();
                echo "<p style='color: green;'>Sample assignment created successfully with ID: " . $assignmentId . "</p>";
            } else {
                echo "<p style='color: red;'>Failed to create sample assignment</p>";
            }
        }
    } else {
        echo "<p style='color: green;'>Found " . $count['count'] . " assignments in the database.</p>";
    }
    
    echo "<p><a href='online_learning/assignments.php'>Go to Assignments Page</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>Error: " . $e->getMessage() . "</p>";
}
?>