<?php
session_start();
require_once '../config.php';
require_once 'blog-functions.php';

// Force admin session for testing
$_SESSION['admin_id'] = 1;
$_SESSION['admin_name'] = 'admin';

echo "🔍 ADMIN DASHBOARD DEBUG TEST\n";
echo "=============================\n\n";

// Test blog statistics
echo "1. Testing Blog Statistics Function...\n";
try {
    $blog_stats = getBlogStats($link);
    echo "   ✅ Blog stats retrieved successfully:\n";
    foreach ($blog_stats as $key => $value) {
        if (is_array($value)) {
            echo "      📊 $key: " . json_encode($value) . "\n";
        } else {
            echo "      📊 $key: $value\n";
        }
    }
} catch (Exception $e) {
    echo "   ❌ Error: " . $e->getMessage() . "\n";
}

// Test admin session
echo "\n2. Testing Admin Session...\n";
if (isset($_SESSION['admin_id']) && isset($_SESSION['admin_name'])) {
    echo "   ✅ Admin session active\n";
    echo "      👤 Admin ID: " . $_SESSION['admin_id'] . "\n";
    echo "      🏷️ Admin Name: " . $_SESSION['admin_name'] . "\n";
} else {
    echo "   ❌ Admin session not found\n";
}

// Test database tables
echo "\n3. Testing Database Tables...\n";
$required_tables = ['blogs', 'contacts', 'admin'];
foreach ($required_tables as $table) {
    $result = mysqli_query($link, "SELECT COUNT(*) as count FROM $table");
    if ($result) {
        $count = mysqli_fetch_assoc($result)['count'];
        echo "   ✅ Table '$table': $count records\n";
    } else {
        echo "   ❌ Table '$table': Error - " . mysqli_error($link) . "\n";
    }
}

// Test blog retrieval
echo "\n4. Testing Blog Retrieval...\n";
try {
    $all_blogs = getAllBlogs($link, 10, 0);
    echo "   ✅ Retrieved " . count($all_blogs) . " blogs\n";
    foreach ($all_blogs as $blog) {
        echo "      📄 " . substr($blog['title'], 0, 40) . "...\n";
    }
} catch (Exception $e) {
    echo "   ❌ Error retrieving blogs: " . $e->getMessage() . "\n";
}

echo "\n✨ Debug test complete!\n";
echo "\n🌐 If all tests pass, admin dashboard should work at:\n";
echo "   http://localhost/A+/01/admin/admin-dashboard.php\n";
?>