<?php
/**
 * تحليل شامل لمشكلة المنشورات
 */

session_start();
require_once '../config/database.php';

echo "<!DOCTYPE html>
<html lang='ar' dir='rtl'>
<head>
    <meta charset='UTF-8'>
    <title>تحليل المشكلة</title>
    <style>
        * { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
        body { background: #f5f5f5; padding: 20px; }
        .container { max-width: 1000px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; }
        h1 { color: #E57393; }
        .section { margin: 20px 0; padding: 20px; border: 1px solid #ddd; border-radius: 5px; }
        .success { background: #d4edda; color: #155724; }
        .error { background: #f8d7da; color: #721c24; }
        .warning { background: #fff3cd; color: #856404; }
        .info { background: #d1ecf1; color: #0c5460; }
        pre { background: #f8f9fa; padding: 10px; border-radius: 5px; overflow-x: auto; }
        .btn { display: inline-block; padding: 10px 20px; background: #E57393; color: white; text-decoration: none; border-radius: 5px; margin: 5px; }
    </style>
</head>
<body>
    <div class='container'>
        <h1>🔍 تحليل شامل لمشكلة المنشورات</h1>";

// 1. فحص الـ Session
echo "<div class='section'>";
echo "<h2>1️⃣ فحص الـ Session</h2>";
if (isset($_SESSION['admin_id'])) {
    echo "<div class='success'>✅ Session موجود - Admin ID: " . $_SESSION['admin_id'] . "</div>";
} else {
    echo "<div class='error'>❌ Session مش موجود - لازم تسجل دخول</div>";
}
echo "</div>";

// 2. فحص الاتصال بقاعدة البيانات
echo "<div class='section'>";
echo "<h2>2️⃣ فحص قاعدة البيانات</h2>";
try {
    $database = new Database();
    $conn = $database->getConnection();
    echo "<div class='success'>✅ الاتصال بقاعدة البيانات شغال</div>";
    
    // 3. فحص وجود جدول posts
    echo "<h3>فحص جدول posts:</h3>";
    $check = $conn->query("SHOW TABLES LIKE 'posts'");
    if ($check->rowCount() > 0) {
        echo "<div class='success'>✅ جدول posts موجود</div>";
        
        // عرض بنية الجدول
        $structure = $conn->query("DESCRIBE posts");
        echo "<h4>بنية الجدول:</h4>";
        echo "<pre>";
        while ($row = $structure->fetch(PDO::FETCH_ASSOC)) {
            echo $row['Field'] . " - " . $row['Type'] . "/n";
        }
        echo "</pre>";
        
        // عدد المنشورات
        $count = $conn->query("SELECT COUNT(*) FROM posts")->fetchColumn();
        echo "<div class='info'>📊 عدد المنشورات: $count</div>";
        
    } else {
        echo "<div class='error'>❌ جدول posts غير موجود!</div>";
        echo "<div class='warning'>⚠️ لازم تثبت النظام الأول</div>";
    }
    
    // 4. فحص جدول post_categories
    echo "<h3>فحص جدول post_categories:</h3>";
    $check = $conn->query("SHOW TABLES LIKE 'post_categories'");
    if ($check->rowCount() > 0) {
        echo "<div class='success'>✅ جدول post_categories موجود</div>";
        $count = $conn->query("SELECT COUNT(*) FROM post_categories")->fetchColumn();
        echo "<div class='info'>📊 عدد الفئات: $count</div>";
    } else {
        echo "<div class='error'>❌ جدول post_categories غير موجود</div>";
    }
    
    // 5. فحص جدول post_comments
    echo "<h3>فحص جدول post_comments:</h3>";
    $check = $conn->query("SHOW TABLES LIKE 'post_comments'");
    if ($check->rowCount() > 0) {
        echo "<div class='success'>✅ جدول post_comments موجود</div>";
    } else {
        echo "<div class='error'>❌ جدول post_comments غير موجود</div>";
    }
    
    // 6. فحص جدول post_likes
    echo "<h3>فحص جدول post_likes:</h3>";
    $check = $conn->query("SHOW TABLES LIKE 'post_likes'");
    if ($check->rowCount() > 0) {
        echo "<div class='success'>✅ جدول post_likes موجود</div>";
    } else {
        echo "<div class='error'>❌ جدول post_likes غير موجود</div>";
    }
    
} catch (Exception $e) {
    echo "<div class='error'>❌ خطأ في الاتصال: " . $e->getMessage() . "</div>";
}
echo "</div>";

// 7. فحص الملفات
echo "<div class='section'>";
echo "<h2>3️⃣ فحص الملفات</h2>";

$files = [
    'posts/index.php' => 'صفحة المنشورات',
    'posts/create.php' => 'صفحة إضافة منشور',
    'install-features.php' => 'صفحة التثبيت',
    '../setup/create_posts_system.php' => 'ملف التثبيت'
];

foreach ($files as $file => $desc) {
    if (file_exists($file)) {
        echo "<div class='success'>✅ $desc موجود: $file</div>";
    } else {
        echo "<div class='error'>❌ $desc غير موجود: $file</div>";
    }
}
echo "</div>";

// 8. فحص المجلدات
echo "<div class='section'>";
echo "<h2>4️⃣ فحص المجلدات</h2>";

$folders = [
    'posts' => 'مجلد المنشورات',
    '../uploads/posts' => 'مجلد رفع الصور'
];

foreach ($folders as $folder => $desc) {
    if (is_dir($folder)) {
        echo "<div class='success'>✅ $desc موجود: $folder</div>";
        if (is_writable($folder)) {
            echo "<div class='info'>📝 المجلد قابل للكتابة</div>";
        } else {
            echo "<div class='warning'>⚠️ المجلد غير قابل للكتابة</div>";
        }
    } else {
        echo "<div class='error'>❌ $desc غير موجود: $folder</div>";
    }
}
echo "</div>";

// 9. التوصيات
echo "<div class='section'>";
echo "<h2>5️⃣ التوصيات والحلول</h2>";

$check_posts = false;
try {
    $check = $conn->query("SHOW TABLES LIKE 'posts'");
    $check_posts = $check->rowCount() > 0;
} catch (Exception $e) {}

if (!$check_posts) {
    echo "<div class='error'>";
    echo "<h3>❌ المشكلة الرئيسية: جدول posts غير موجود!</h3>";
    echo "<p><strong>الحل:</strong></p>";
    echo "<ol>";
    echo "<li>افتح ملف التثبيت مباشرة</li>";
    echo "<li>انتظر حتى ينتهي التثبيت</li>";
    echo "<li>ارجع لصفحة المنشورات</li>";
    echo "</ol>";
    echo "<a href='../setup/create_posts_system.php' class='btn'>🔧 تثبيت النظام الآن</a>";
    echo "</div>";
} else {
    echo "<div class='success'>";
    echo "<h3>✅ كل شيء تمام!</h3>";
    echo "<p>الجداول موجودة والنظام جاهز للاستخدام</p>";
    echo "<a href='posts/index.php' class='btn'>📝 فتح المنشورات</a>";
    echo "</div>";
}

echo "</div>";

// 10. معلومات إضافية
echo "<div class='section info'>";
echo "<h2>6️⃣ معلومات إضافية</h2>";
echo "<p><strong>المسار الحالي:</strong> " . __DIR__ . "</p>";
echo "<p><strong>PHP Version:</strong> " . phpversion() . "</p>";
echo "<p><strong>Server:</strong> " . $_SERVER['SERVER_SOFTWARE'] . "</p>";
echo "</div>";

// روابط سريعة
echo "<div class='section'>";
echo "<h2>🔗 روابط سريعة</h2>";
echo "<a href='dashboard.php' class='btn'>🏠 الداشبورد</a>";
echo "<a href='install-features.php' class='btn'>🚀 صفحة التثبيت</a>";
echo "<a href='../setup/create_posts_system.php' class='btn'>🔧 تثبيت مباشر</a>";
echo "<a href='posts/test.php' class='btn'>🧪 اختبار المجلد</a>";
echo "</div>";

echo "</div>
</body>
</html>";
?>
