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

echo "<!DOCTYPE html>";
echo "<html lang='bn'>";
echo "<head>";
echo "<meta charset='UTF-8'>";
echo "<title>আপলোড সিস্টেম সেটআপ</title>";
echo "<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>";
echo "<link href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css' rel='stylesheet'>";
echo "</head>";
echo "<body class='bg-light'>";

echo "<div class='container mt-4'>";
echo "<div class='row justify-content-center'>";
echo "<div class='col-md-8'>";

echo "<div class='card'>";
echo "<div class='card-header bg-primary text-white text-center'>";
echo "<h3><i class='fas fa-cloud-upload-alt'></i> ফাইল আপলোড সিস্টেম সেটআপ</h3>";
echo "</div>";
echo "<div class='card-body'>";

try {
    // Create file_uploads table
    $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 class='text-success'><i class='fas fa-check'></i> File Uploads টেবিল তৈরি সফল</p>";
    
    // Create 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 class='text-success'><i class='fas fa-check'></i> Assignments টেবিল তৈরি সফল</p>";
    
    // Create 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 class='text-success'><i class='fas fa-check'></i> Assignment Submissions টেবিল তৈরি সফল</p>";
    
    // Create upload directories
    $upload_dirs = [
        'uploads',
        'uploads/students',
        'uploads/teachers',
        'uploads/assignments',
        'uploads/documents',
        'uploads/temp'
    ];
    
    foreach ($upload_dirs as $dir) {
        if (!is_dir($dir)) {
            if (mkdir($dir, 0777, true)) {
                echo "<p class='text-success'><i class='fas fa-folder'></i> $dir ডিরেক্টরি তৈরি সফল</p>";
            } else {
                echo "<p class='text-warning'><i class='fas fa-exclamation-triangle'></i> $dir ডিরেক্টরি তৈরি করতে সমস্যা</p>";
            }
        } else {
            echo "<p class='text-info'><i class='fas fa-folder-open'></i> $dir ডিরেক্টরি বিদ্যমান</p>";
        }
    }
    
    // Create .htaccess for security
    $htaccess_content = "
# Prevent direct access to PHP files in upload directories
<Files *.php>
    Order Deny,Allow
    Deny from all
</Files>

# Prevent execution of scripts
Options -ExecCGI
AddHandler cgi-script .php .pl .py .jsp .asp .sh .cgi
";
    
    foreach (['uploads/students', 'uploads/teachers', 'uploads/assignments'] as $dir) {
        $htaccess_file = $dir . '/.htaccess';
        if (!file_exists($htaccess_file)) {
            file_put_contents($htaccess_file, $htaccess_content);
            echo "<p class='text-success'><i class='fas fa-shield-alt'></i> $dir এ নিরাপত্তা ফাইল তৈরি</p>";
        }
    }
    
    // Check PHP configuration
    echo "<hr>";
    echo "<h5><i class='fas fa-cog'></i> PHP কনফিগারেশন চেক</h5>";
    
    $upload_max = ini_get('upload_max_filesize');
    $post_max = ini_get('post_max_size');
    $max_execution = ini_get('max_execution_time');
    
    echo "<div class='row'>";
    echo "<div class='col-md-4'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Upload Max Size</h6>";
    echo "<h4 class='text-primary'>$upload_max</h4>";
    if (intval($upload_max) >= 10) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম (১০M+ প্রয়োজন)</small>";
    }
    echo "</div></div></div>";
    
    echo "<div class='col-md-4'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Post Max Size</h6>";
    echo "<h4 class='text-primary'>$post_max</h4>";
    if (intval($post_max) >= 12) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম (১২M+ প্রয়োজন)</small>";
    }
    echo "</div></div></div>";
    
    echo "<div class='col-md-4'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Max Execution Time</h6>";
    echo "<h4 class='text-primary'>{$max_execution}s</h4>";
    if (intval($max_execution) >= 300 || $max_execution == 0) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম (৩০০s+ প্রয়োজন)</small>";
    }
    echo "</div></div></div>";
    echo "</div>";
    
    echo "<hr>";
    echo "<div class='alert alert-success text-center'>";
    echo "<h4><i class='fas fa-check-circle'></i> সেটআপ সম্পন্ন!</h4>";
    echo "<p>ফাইল আপলোড সিস্টেম ব্যবহারের জন্য প্রস্তুত।</p>";
    echo "</div>";
    
    echo "<div class='text-center'>";
    echo "<h5>এখন আপনি ব্যবহার করতে পারেন:</h5>";
    echo "<div class='d-flex justify-content-center flex-wrap gap-2 mt-3'>";
    echo "<a href='uploads/index.php' class='btn btn-primary'><i class='fas fa-home'></i> আপলোড হোম</a>";
    echo "<a href='uploads/upload.php' class='btn btn-success'><i class='fas fa-plus'></i> ফাইল আপলোড</a>";
    echo "<a href='uploads/my_files.php' class='btn btn-info'><i class='fas fa-list'></i> আমার ফাইল</a>";
    echo "<a href='index.php' class='btn btn-warning'><i class='fas fa-arrow-left'></i> মূল সিস্টেম</a>";
    echo "</div>";
    echo "</div>";
    
    echo "<hr>";
    echo "<div class='alert alert-info'>";
    echo "<h6><i class='fas fa-info-circle'></i> গুরুত্বপূর্ণ তথ্য:</h6>";
    echo "<ul class='mb-0'>";
    echo "<li>সমর্থিত ফাইল: PDF, DOC, DOCX, JPG, PNG, GIF, MP4, AVI, TXT</li>";
    echo "<li>সর্বোচ্চ ফাইল সাইজ: ১০MB</li>";
    echo "<li>শিক্ষকরা পাবলিক ফাইল শেয়ার করতে পারেন</li>";
    echo "<li>ছাত্ররা শিক্ষকদের পাবলিক ফাইল দেখতে পারেন</li>";
    echo "</ul>";
    echo "</div>";
    
} catch (Exception $e) {
    echo "<div class='alert alert-danger'>";
    echo "<h5><i class='fas fa-exclamation-triangle'></i> ত্রুটি!</h5>";
    echo "<p>" . $e->getMessage() . "</p>";
    echo "</div>";
}

echo "</div>"; // card-body
echo "</div>"; // card
echo "</div>"; // col
echo "</div>"; // row
echo "</div>"; // container

echo "</body>";
echo "</html>";
?>

<style>
.card { box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); }
.btn { margin: 2px; }
</style>