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

try {
    // File uploads table for students and teachers
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS file_uploads (
            id INT AUTO_INCREMENT PRIMARY KEY,
            uploader_type ENUM('student', 'teacher', 'admin') NOT NULL,
            uploader_id INT NOT NULL,
            file_name VARCHAR(255) NOT NULL,
            original_name VARCHAR(255) NOT NULL,
            file_path VARCHAR(500) NOT NULL,
            file_type VARCHAR(100) NOT NULL,
            file_size INT NOT NULL,
            category ENUM('assignment', 'document', 'image', 'video', 'other') DEFAULT 'document',
            description TEXT,
            is_public BOOLEAN DEFAULT FALSE,
            target_class_id INT NULL,
            target_subject_id INT NULL,
            status ENUM('active', 'inactive', 'deleted') DEFAULT 'active',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_uploader (uploader_type, uploader_id),
            INDEX idx_category (category),
            INDEX idx_target (target_class_id, target_subject_id)
        )
    ");
    echo "<p style='color: green;'>✓ File Uploads টেবিল তৈরি সফল</p>";
    
    // Assignment submissions table
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS assignment_submissions (
            id INT AUTO_INCREMENT PRIMARY KEY,
            assignment_id INT NOT NULL,
            student_id INT NOT NULL,
            file_upload_id INT NOT NULL,
            submission_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            grade DECIMAL(5,2) NULL,
            feedback TEXT,
            status ENUM('submitted', 'graded', 'returned') DEFAULT 'submitted',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            FOREIGN KEY (file_upload_id) REFERENCES file_uploads(id) ON DELETE CASCADE,
            UNIQUE KEY unique_assignment_student (assignment_id, student_id)
        )
    ");
    echo "<p style='color: green;'>✓ Assignment Submissions টেবিল তৈরি সফল</p>";
    
    // Assignments table
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS assignments (
            id INT AUTO_INCREMENT PRIMARY KEY,
            title VARCHAR(200) NOT NULL,
            description TEXT,
            class_id INT NOT NULL,
            subject_id INT NOT NULL,
            teacher_id INT NOT NULL,
            due_date DATETIME NOT NULL,
            max_marks DECIMAL(5,2) DEFAULT 100,
            file_upload_id INT NULL,
            instructions TEXT,
            status ENUM('draft', 'published', 'closed') DEFAULT 'draft',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE,
            FOREIGN KEY (subject_id) REFERENCES subjects(id) ON DELETE CASCADE,
            FOREIGN KEY (teacher_id) REFERENCES teachers(id) ON DELETE CASCADE,
            FOREIGN KEY (file_upload_id) REFERENCES file_uploads(id) ON DELETE SET NULL
        )
    ");
    echo "<p style='color: green;'>✓ Assignments টেবিল তৈরি সফল</p>";
    
    // Create upload directories
    $upload_dirs = [
        'uploads/students',
        'uploads/teachers',
        'uploads/assignments',
        'uploads/documents',
        'uploads/temp'
    ];
    
    foreach ($upload_dirs as $dir) {
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
            echo "<p style='color: green;'>✓ $dir ডিরেক্টরি তৈরি</p>";
        } else {
            echo "<p style='color: blue;'>✓ $dir ডিরেক্টরি বিদ্যমান</p>";
        }
    }
    
    echo "<h3 style='color: green;'>✅ আপলোড সিস্টেম সেটআপ সম্পন্ন!</h3>";
    echo "<p><a href='uploads/index.php' class='btn btn-primary'>আপলোড সিস্টেম দেখুন</a></p>";
    
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ ত্রুটি: " . $e->getMessage() . "</p>";
}
?>

<style>
body { font-family: 'SolaimanLipi', Arial, sans-serif; margin: 20px; }
.btn { padding: 8px 16px; margin: 5px; text-decoration: none; border-radius: 4px; display: inline-block; background-color: #007bff; color: white; }
</style>