<?php
require_once 'db_config.php';

echo "<h2>Blog Database Check</h2>";

// Check if blogs table exists
$result = $conn->query("SHOW TABLES LIKE 'blogs'");
if ($result && $result->num_rows > 0) {
    echo "<p style='color:green'>✓ blogs table exists</p>";
    
    // Count blogs
    $result = $conn->query("SELECT COUNT(*) as count FROM blogs");
    $row = $result->fetch_assoc();
    echo "<p>Total blogs: <strong>" . $row['count'] . "</strong></p>";
    
    // Count published blogs
    $result = $conn->query("SELECT COUNT(*) as count FROM blogs WHERE status = 'published'");
    $row = $result->fetch_assoc();
    echo "<p>Published blogs: <strong>" . $row['count'] . "</strong></p>";
    
    // Count blogs with show_on_home = 1
    $result = $conn->query("SELECT COUNT(*) as count FROM blogs WHERE show_on_home = 1 AND status = 'published'");
    $row = $result->fetch_assoc();
    echo "<p>Blogs marked to show on home: <strong>" . $row['count'] . "</strong></p>";
    
    // List all blogs
    echo "<h3>All Blogs:</h3>";
    $result = $conn->query("SELECT id, title, slug, status, show_on_home, publish_date, created_at FROM blogs ORDER BY created_at DESC");
    
    if ($result && $result->num_rows > 0) {
        echo "<table border='1' style='border-collapse:collapse; margin-top:10px; width:100%;'>";
        echo "<tr><th>ID</th><th>Title</th><th>Slug</th><th>Status</th><th>Show on Home</th><th>Publish Date</th><th>Created</th></tr>";
        while ($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row['id'] . "</td>";
            echo "<td>" . htmlspecialchars($row['title']) . "</td>";
            echo "<td>" . htmlspecialchars($row['slug']) . "</td>";
            echo "<td><strong>" . $row['status'] . "</strong></td>";
            echo "<td>" . ($row['show_on_home'] ? 'Yes' : 'No') . "</td>";
            echo "<td>" . ($row['publish_date'] ?? 'Not set') . "</td>";
            echo "<td>" . $row['created_at'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p style='color:orange'>⚠ No blogs found in database</p>";
    }
} else {
    echo "<p style='color:red'>✗ blogs table does not exist</p>";
    echo "<p><a href='run_blog_migration.php'>Create blog tables</a></p>";
}

// Check categories
echo "<h3>Blog Categories:</h3>";
$result = $conn->query("SELECT * FROM blog_categories");
if ($result && $result->num_rows > 0) {
    echo "<table border='1' style='border-collapse:collapse; margin-top:10px;'>";
    echo "<tr><th>ID</th><th>Name</th><th>Slug</th></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['slug'] . "</td></tr>";
    }
    echo "</table>";
} else {
    echo "<p>No categories found</p>";
}

echo "<hr>";
echo "<p><a href='admin_add_blog.php' style='background:#c76b8a;color:white;padding:10px;border-radius:4px;text-decoration:none;'>+ Add New Blog Post</a></p>";
echo "<p><a href='admin_blogs.php' style='background:#3498db;color:white;padding:10px;border-radius:4px;text-decoration:none;'>Manage Blogs</a></p>";
?>
