<?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-success text-white text-center'>";
echo "<h3><i class='fas fa-users'></i> বাল্ক আপলোড সিস্টেম সেটআপ</h3>";
echo "</div>";
echo "<div class='card-body'>";

try {
    // Create bulk_upload_logs table for tracking
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS bulk_upload_logs (
            id INT AUTO_INCREMENT PRIMARY KEY,
            upload_type ENUM('students', 'teachers', 'classes', 'subjects') NOT NULL,
            file_name VARCHAR(255) NOT NULL,
            total_records INT NOT NULL,
            successful_records INT NOT NULL,
            failed_records INT NOT NULL,
            uploaded_by INT NOT NULL,
            upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            status ENUM('completed', 'failed', 'partial') NOT NULL,
            error_details TEXT,
            FOREIGN KEY (uploaded_by) REFERENCES users(id)
        )
    ");
    echo "<p class='text-success'><i class='fas fa-check'></i> Bulk Upload Logs টেবিল তৈরি সফল</p>";
    
    // Create directories
    $directories = [
        'bulk_upload',
        'bulk_upload/templates',
        'bulk_upload/uploads',
        'bulk_upload/logs'
    ];
    
    foreach ($directories 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>";
        }
    }
    
    // Check if template files exist
    $template_files = [
        'bulk_upload/templates/students_template.csv' => 'শিক্ষার্থী টেমপ্লেট',
        'bulk_upload/templates/teachers_template.csv' => 'শিক্ষক টেমপ্লেট'
    ];
    
    foreach ($template_files as $file => $description) {
        if (file_exists($file)) {
            echo "<p class='text-success'><i class='fas fa-file-csv'></i> $description বিদ্যমান</p>";
        } else {
            echo "<p class='text-warning'><i class='fas fa-exclamation-triangle'></i> $description পাওয়া যায়নি</p>";
        }
    }
    
    // Check PHP configuration for file uploads
    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');
    $memory_limit = ini_get('memory_limit');
    
    echo "<div class='row'>";
    
    echo "<div class='col-md-3'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Upload Max</h6>";
    echo "<h5 class='text-primary'>$upload_max</h5>";
    if (intval($upload_max) >= 2) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম</small>";
    }
    echo "</div></div></div>";
    
    echo "<div class='col-md-3'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Post Max</h6>";
    echo "<h5 class='text-primary'>$post_max</h5>";
    if (intval($post_max) >= 4) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম</small>";
    }
    echo "</div></div></div>";
    
    echo "<div class='col-md-3'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Execution Time</h6>";
    echo "<h5 class='text-primary'>{$max_execution}s</h5>";
    if (intval($max_execution) >= 300 || $max_execution == 0) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম</small>";
    }
    echo "</div></div></div>";
    
    echo "<div class='col-md-3'>";
    echo "<div class='card bg-light'>";
    echo "<div class='card-body text-center'>";
    echo "<h6>Memory Limit</h6>";
    echo "<h5 class='text-primary'>$memory_limit</h5>";
    if (intval($memory_limit) >= 128) {
        echo "<small class='text-success'>✓ ভাল</small>";
    } else {
        echo "<small class='text-warning'>⚠ কম</small>";
    }
    echo "</div></div></div>";
    
    echo "</div>";
    
    // Sample data counts
    echo "<hr>";
    echo "<h5><i class='fas fa-chart-bar'></i> বর্তমান ডেটা পরিসংখ্যান</h5>";
    
    $stats = [
        'students' => $pdo->query("SELECT COUNT(*) FROM students")->fetchColumn(),
        'teachers' => $pdo->query("SELECT COUNT(*) FROM teachers")->fetchColumn(),
        'classes' => $pdo->query("SELECT COUNT(*) FROM classes")->fetchColumn(),
        'subjects' => $pdo->query("SELECT COUNT(*) FROM subjects")->fetchColumn()
    ];
    
    echo "<div class='row'>";
    foreach ($stats as $type => $count) {
        $labels = [
            'students' => ['শিক্ষার্থী', 'success'],
            'teachers' => ['শিক্ষক', 'info'],
            'classes' => ['ক্লাস', 'warning'],
            'subjects' => ['বিষয়', 'primary']
        ];
        
        echo "<div class='col-md-3'>";
        echo "<div class='card bg-{$labels[$type][1]} text-white'>";
        echo "<div class='card-body text-center'>";
        echo "<h3>$count</h3>";
        echo "<p class='mb-0'>{$labels[$type][0]}</p>";
        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='bulk_upload/index.php' class='btn btn-primary'><i class='fas fa-home'></i> বাল্ক আপলোড হোম</a>";
    echo "<a href='bulk_upload/students_upload.php' class='btn btn-success'><i class='fas fa-user-graduate'></i> শিক্ষার্থী আপলোড</a>";
    echo "<a href='bulk_upload/teachers_upload.php' class='btn btn-info'><i class='fas fa-chalkboard-teacher'></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>CSV ফাইল UTF-8 এনকোডিং এ সেভ করুন</li>";
    echo "<li>বড় ফাইলের জন্য PHP timeout বাড়ান</li>";
    echo "<li>আপলোডের আগে ব্যাকআপ নিন</li>";
    echo "<li>টেমপ্লেট অনুযায়ী ডেটা পূরণ করুন</li>";
    echo "<li>ডুপ্লিকেট ID এড়িয়ে চলুন</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>