<?php
/**
 * فحص وتثبيت نظام المنشورات تلقائياً
 */

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

// التحقق من تسجيل الدخول
if (!isset($_SESSION['admin_id'])) {
    header('Location: ../login.php');
    exit;
}

$database = new Database();
$conn = $database->getConnection();

// فحص وجود جدول posts
try {
    $check = $conn->query("SHOW TABLES LIKE 'posts'");
    $table_exists = $check->rowCount() > 0;
    
    if (!$table_exists) {
        // تشغيل ملف الإنشاء
        header('Location: ../../setup/create_posts_system.php');
        exit;
    } else {
        // الجدول موجود، الذهاب لصفحة المنشورات
        header('Location: index.php');
        exit;
    }
} catch (Exception $e) {
    die("خطأ: " . $e->getMessage());
}
?>
