<?php
require_once 'db_config.php';
require_once 'blog_helpers.php';

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

// Count all blogs
$result = $conn->query("SELECT COUNT(*) as count FROM blogs");
$row = $result->fetch_assoc();
echo "<p><strong>Total blogs in database:</strong> " . $row['count'] . "</p>";

// List ALL blogs with full details
echo "<h3>All Blogs (no filters):</h3>";
$result = $conn->query("
    SELECT b.id, b.title, b.slug, b.status, b.show_on_home, b.publish_date, 
           b.created_at, b.featured_image, bc.name as category_name
    FROM blogs b
    LEFT JOIN blog_categories bc ON b.category_id = bc.id
    ORDER BY b.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>Category</th><th>Image</th><th>Created</th></tr>";
    while ($row = $result->fetch_assoc()) {
        $hasImage = !empty($row['featured_image']) ? '✓' : '✗';
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . htmlspecialchars(substr($row['title'], 0, 30)) . "</td>";
        echo "<td>" . htmlspecialchars($row['slug']) . "</td>";
        echo "<td><strong style='color:" . ($row['status'] === 'published' ? 'green' : 'orange') . "'>" . $row['status'] . "</strong></td>";
        echo "<td style='text-align:center'>" . ($row['show_on_home'] ? '✓' : '✗') . "</td>";
        echo "<td>" . ($row['publish_date'] ? $row['publish_date'] : '<em>Not set</em>') . "</td>";
        echo "<td>" . ($row['category_name'] ?? 'N/A') . "</td>";
        echo "<td style='text-align:center'>" . $hasImage . "</td>";
        echo "<td>" . $row['created_at'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p style='color:red'>No blogs found</p>";
}

// Test getBlogs function
echo "<h3>Testing getBlogs() function:</h3>";
$testBlogs = getBlogs($conn, ['show_on_home' => 1, 'limit' => 10]);
echo "<p><strong>Blogs with show_on_home=1:</strong> " . count($testBlogs) . "</p>";
if (!empty($testBlogs)) {
    foreach ($testBlogs as $blog) {
        echo "<p>- " . htmlspecialchars($blog['title']) . " (Status: " . $blog['status'] . ")</p>";
    }
}

// Test without filters
echo "<h3>Testing getBlogs() without filters:</h3>";
$allBlogs = getBlogs($conn, ['limit' => 10]);
echo "<p><strong>Total blogs returned:</strong> " . count($allBlogs) . "</p>";
if (!empty($allBlogs)) {
    foreach ($allBlogs as $blog) {
        echo "<p>- " . htmlspecialchars($blog['title']) . " (show_on_home: " . $blog['show_on_home'] . ", status: " . $blog['status'] . ")</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>";
echo "<p><a href='index.php' style='background:#27ae60;color:white;padding:10px;border-radius:4px;text-decoration:none;'>View Homepage</a></p>";
?>
