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

header('Content-Type: application/json');

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Authentication required']);
    exit();
}

try {
    $user_id = $_SESSION['user_id'];
    $user_role = $_SESSION['user_role'];
    
    // Get assignments based on user role
    if ($user_role == 'admin' || $user_role == 'teacher') {
        // For teachers/admins, get all assignments they created
        $stmt = $pdo->prepare("
            SELECT a.*, c.name as class_name, s.name as subject_name, u.username as teacher_name
            FROM assignments a
            JOIN classes c ON a.class_id = c.id
            JOIN subjects s ON a.subject_id = s.id
            JOIN teachers t ON a.teacher_id = t.id
            JOIN users u ON t.user_id = u.id
            WHERE a.teacher_id = (
                SELECT id FROM teachers WHERE user_id = ?
            )
            ORDER BY a.created_at DESC
        ");
        $stmt->execute([$user_id]);
    } else {
        // For students, get assignments for their class
        $stmt = $pdo->prepare("
            SELECT a.*, c.name as class_name, s.name as subject_name, u.username as teacher_name
            FROM assignments a
            JOIN classes c ON a.class_id = c.id
            JOIN subjects s ON a.subject_id = s.id
            JOIN teachers t ON a.teacher_id = t.id
            JOIN users u ON t.user_id = u.id
            WHERE a.class_id = (
                SELECT class_id FROM students WHERE user_id = ?
            )
            AND a.status = 'published'
            ORDER BY a.created_at DESC
        ");
        $stmt->execute([$user_id]);
    }
    
    $assignments = $stmt->fetchAll();
    
    // Add submission status for each assignment (for students)
    if ($user_role == 'student') {
        foreach ($assignments as &$assignment) {
            $stmt = $pdo->prepare("
                SELECT status, grade, submission_date 
                FROM assignment_submissions 
                WHERE assignment_id = ? AND student_id = (
                    SELECT id FROM students WHERE user_id = ?
                )
            ");
            $stmt->execute([$assignment['id'], $user_id]);
            $submission = $stmt->fetch();
            
            if ($submission) {
                $assignment['submission_status'] = $submission['status'];
                $assignment['grade'] = $submission['grade'];
                $assignment['submission_date'] = $submission['submission_date'];
            } else {
                $assignment['submission_status'] = 'not_submitted';
                $assignment['grade'] = null;
                $assignment['submission_date'] = null;
            }
        }
    }
    
    echo json_encode(['success' => true, 'assignments' => $assignments]);
    
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error fetching assignments: ' . $e->getMessage()]);
}
?>