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

try {
    // Check if we have any teachers
    $stmt = $pdo->query("SELECT id FROM teachers LIMIT 1");
    $teacher = $stmt->fetch();
    
    if (!$teacher) {
        echo "No teachers found. Creating a sample teacher first...\n";
        
        // Create a sample teacher
        $stmt = $pdo->prepare("INSERT INTO users (username, password, email, role) VALUES (?, ?, ?, ?)");
        $password = password_hash('teacher123', PASSWORD_DEFAULT);
        $stmt->execute(['teacher1', $password, 'teacher1@school.com', 'teacher']);
        $userId = $pdo->lastInsertId();
        
        $stmt = $pdo->prepare("INSERT INTO teachers (user_id, employee_id, first_name, last_name, email) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$userId, 'T001', 'জন', 'ডেভেলপার', 'teacher1@school.com']);
        $teacherId = $pdo->lastInsertId();
    } else {
        $teacherId = $teacher['id'];
        echo "Found teacher with ID: " . $teacherId . "\n";
    }
    
    // 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 (গণিত)
        $teacherId, // teacher_id
        '2025-10-25 23:59:59', // due_date
        20, // max_marks
        'published' // status
    ]);
    
    if ($result) {
        $assignmentId = $pdo->lastInsertId();
        echo "Sample assignment created successfully with ID: " . $assignmentId . "\n";
    } else {
        echo "Failed to create sample assignment\n";
    }
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>