<?php
/**
 * تثبيت سريع للمنشورات والاستوريز
 */

session_start();
require_once '../config/database.php';

// التحقق من تسجيل الدخول
if (!isset($_SESSION['admin_id'])) {
    header('Location: login.php');
    exit;
}

$database = new Database();
$conn = $database->getConnection();

$installed = [];
$errors = [];
$install_type = $_GET['install'] ?? 'all';

// فحص وتثبيت المنشورات
if ($install_type == 'all' || $install_type == 'posts') {
    try {
        $check = $conn->query("SHOW TABLES LIKE 'posts'");
        if ($check->rowCount() == 0) {
            // تثبيت المنشورات
            ob_start();
            include '../setup/create_posts_system.php';
            ob_end_clean();
            $installed[] = 'المنشورات';
        } else {
            if ($install_type == 'posts') {
                header('Location: posts/index.php');
                exit;
            }
        }
    } catch (Exception $e) {
        $errors[] = 'المنشورات: ' . $e->getMessage();
    }
}

// فحص وتثبيت الاستوريز
if ($install_type == 'all' || $install_type == 'stories') {
    try {
        $check = $conn->query("SHOW TABLES LIKE 'stories'");
        if ($check->rowCount() == 0) {
            // تثبيت الاستوريز
            ob_start();
            include '../setup/create_stories_system.php';
            ob_end_clean();
            $installed[] = 'الاستوريز';
        } else {
            if ($install_type == 'stories') {
                header('Location: stories/index.php');
                exit;
            }
        }
    } catch (Exception $e) {
        $errors[] = 'الاستوريز: ' . $e->getMessage();
    }
}

?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>تثبيت سريع</title>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;700&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Tajawal', sans-serif; margin: 0; padding: 0; box-sizing: border-box; }
        body { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
        .container { background: white; border-radius: 20px; padding: 40px; max-width: 600px; width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,0.3); }
        h1 { color: #667eea; margin-bottom: 30px; text-align: center; font-size: 32px; }
        .success { background: #d4edda; color: #155724; padding: 15px; border-radius: 10px; margin-bottom: 15px; border-left: 4px solid #28a745; }
        .error { background: #f8d7da; color: #721c24; padding: 15px; border-radius: 10px; margin-bottom: 15px; border-left: 4px solid #dc3545; }
        .info { background: #d1ecf1; color: #0c5460; padding: 15px; border-radius: 10px; margin-bottom: 15px; border-left: 4px solid #17a2b8; }
        .btn { display: inline-block; padding: 15px 30px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; text-decoration: none; border-radius: 50px; font-weight: 600; margin-top: 20px; transition: transform 0.3s; }
        .btn:hover { transform: translateY(-2px); }
        ul { list-style: none; padding: 0; }
        li { padding: 10px; margin: 5px 0; background: #f8f9fa; border-radius: 5px; }
        li:before { content: "✓ "; color: #28a745; font-weight: bold; margin-left: 10px; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚀 تثبيت سريع</h1>
        
        <?php if (!empty($installed)): ?>
            <div class="success">
                <h3 style="margin-bottom: 10px;">✅ تم التثبيت بنجاح!</h3>
                <ul>
                    <?php foreach ($installed as $item): ?>
                        <li><?php echo $item; ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        
        <?php if (!empty($errors)): ?>
            <div class="error">
                <h3 style="margin-bottom: 10px;">❌ حدثت أخطاء:</h3>
                <ul>
                    <?php foreach ($errors as $error): ?>
                        <li><?php echo $error; ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
        
        <?php if (empty($installed) && empty($errors)): ?>
            <div class="info">
                <h3 style="margin-bottom: 10px;">ℹ️ كل شيء مثبت بالفعل!</h3>
                <p>جميع الأنظمة موجودة ومثبتة بشكل صحيح.</p>
            </div>
        <?php endif; ?>
        
        <div style="text-align: center;">
            <?php if (!empty($installed)): ?>
                <?php if (in_array('المنشورات', $installed)): ?>
                    <a href="posts/index.php" class="btn" style="margin: 5px;">📝 فتح المنشورات</a>
                <?php endif; ?>
                <?php if (in_array('الاستوريز', $installed)): ?>
                    <a href="stories/index.php" class="btn" style="margin: 5px;">🔄 فتح الاستوريز</a>
                <?php endif; ?>
            <?php endif; ?>
            <a href="dashboard.php" class="btn" style="margin: 5px; background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);">← العودة للداشبورد</a>
        </div>
    </div>
</body>
</html>
