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

// Check if user is logged in and is admin/teacher
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] !== 'admin' && $_SESSION['user_role'] !== 'teacher')) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

try {
    // Create a sample assignment for testing
    $stmt = $pdo->prepare("
        INSERT INTO assignments (title, description, class_id, subject_id, teacher_id, due_date, max_marks, status) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
    ");
    
    // Get a teacher ID for testing
    $teacherStmt = $pdo->query("SELECT id FROM teachers LIMIT 1");
    $teacher = $teacherStmt->fetch();
    
    if (!$teacher) {
        echo json_encode(['success' => false, 'message' => 'No teacher found']);
        exit();
    }
    
    $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 json_encode(['success' => true, 'message' => 'Sample assignment created successfully', 'assignment_id' => $assignmentId]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Failed to create sample assignment']);
    }
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>