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

echo "<!DOCTYPE html>
<html>
<head>
    <title>Homepage Blog Debug</title>
    <style>
        body { font-family: Arial; margin: 20px; background: #f5f5f5; }
        .container { max-width: 900px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; }
        .test { margin: 20px 0; padding: 15px; border-left: 4px solid #3498db; background: #ecf7ff; }
        .pass { border-left-color: #27ae60; background: #e8f8f5; }
        .fail { border-left-color: #e74c3c; background: #fadbd8; }
        .test h3 { margin: 0 0 10px 0; }
        table { width: 100%; border-collapse: collapse; margin: 10px 0; }
        table th, table td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        table th { background: #3498db; color: white; }
        code { background: #f4f4f4; padding: 2px 6px; border-radius: 3px; }
        .status-pass { color: green; font-weight: bold; }
        .status-fail { color: red; font-weight: bold; }
        .btn { display: inline-block; padding: 10px 15px; margin: 5px; border-radius: 4px; text-decoration: none; color: white; background: #3498db; }
        .warning { background: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin: 10px 0; }
    </style>
</head>
<body>
    <div class='container'>
        <h1>🐛 Homepage Blog Debug</h1>
";

// TEST 1: Check if index.php includes blog section
echo "<div class='test'>";
echo "<h3>Test 1: index.php File Check</h3>";
$indexContent = file_get_contents('index.php');
if (strpos($indexContent, 'home_blog_section.php') !== false) {
    echo "<p class='status-pass'>✓ PASS: home_blog_section.php is included in index.php</p>";
} else {
    echo "<p class='status-fail'>✗ FAIL: home_blog_section.php NOT included in index.php</p>";
    echo "<div class='warning'><strong>FIX NEEDED:</strong> Add this line before &lt;/main&gt; in index.php:<br><code>&lt;?php require_once 'home_blog_section.php'; ?&gt;</code></div>";
}
echo "</div>";

// TEST 2: Check database for published + show_on_home blogs
echo "<div class='test'>";
echo "<h3>Test 2: Database Check - Published & Show on Home</h3>";

$result = $conn->query("
    SELECT COUNT(*) as total 
    FROM blogs 
    WHERE status = 'published' AND show_on_home = 1 
    AND (publish_date IS NULL OR publish_date <= NOW())
");
$row = $result->fetch_assoc();
$homeBlogs = $row['total'];

echo "<p>Blogs that should appear on homepage: <strong>$homeBlogs</strong></p>";

if ($homeBlogs > 0) {
    echo "<p class='status-pass'>✓ PASS: Found $homeBlogs blogs for homepage</p>";
    
    // List them
    $result = $conn->query("
        SELECT id, title, status, show_on_home, publish_date, featured_image 
        FROM blogs 
        WHERE status = 'published' AND show_on_home = 1 
        AND (publish_date IS NULL OR publish_date <= NOW())
        ORDER BY publish_date DESC
    ");
    
    echo "<table>";
    echo "<tr><th>ID</th><th>Title</th><th>Publish Date</th><th>Image</th></tr>";
    while ($row = $result->fetch_assoc()) {
        $image = $row['featured_image'] ? '✓' : '✗';
        $pubDate = $row['publish_date'] ?? 'Immediate';
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . htmlspecialchars(substr($row['title'], 0, 40)) . "</td>";
        echo "<td>$pubDate</td>";
        echo "<td style='text-align:center;'>$image</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p class='status-fail'>✗ FAIL: No blogs found with both published status AND show_on_home checked</p>";
    echo "<div class='warning'><strong>ACTION NEEDED:</strong><br>";
    echo "1. Go to <a href='admin_blogs.php'>Manage Blogs</a><br>";
    echo "2. Click Edit on a blog<br>";
    echo "3. Make sure:<br>";
    echo "   - Status is set to 'Publish'<br>";
    echo "   - Check the 'Show on Homepage' checkbox<br>";
    echo "   - Leave Publish Date empty OR set it to today<br>";
    echo "4. Click Save</div>";
}

echo "</div>";

// TEST 3: Check all blogs (published only, any show_on_home value)
echo "<div class='test'>";
echo "<h3>Test 3: All Published Blogs (any homepage setting)</h3>";

$result = $conn->query("
    SELECT COUNT(*) as total 
    FROM blogs 
    WHERE status = 'published' AND (publish_date IS NULL OR publish_date <= NOW())
");
$row = $result->fetch_assoc();
$publishedBlogs = $row['total'];

echo "<p>Total published blogs: <strong>$publishedBlogs</strong></p>";

if ($publishedBlogs > 0) {
    echo "<p class='status-pass'>✓ PASS: Found published blogs</p>";
    
    $result = $conn->query("
        SELECT id, title, show_on_home, publish_date 
        FROM blogs 
        WHERE status = 'published' AND (publish_date IS NULL OR publish_date <= NOW())
        ORDER BY created_at DESC
    ");
    
    echo "<table>";
    echo "<tr><th>ID</th><th>Title</th><th>Show on Home</th><th>Publish Date</th></tr>";
    while ($row = $result->fetch_assoc()) {
        $showHome = $row['show_on_home'] ? '✓ YES' : '✗ NO';
        $pubDate = $row['publish_date'] ?? 'Immediate';
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . htmlspecialchars(substr($row['title'], 0, 40)) . "</td>";
        echo "<td style='text-align:center;'>$showHome</td>";
        echo "<td>$pubDate</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p class='status-fail'>✗ FAIL: No published blogs found</p>";
    echo "<div class='warning'><strong>ACTION NEEDED:</strong> <a href='admin_add_blog.php'>Create a blog post</a> and publish it</div>";
}

echo "</div>";

// TEST 4: Test getBlogs function
echo "<div class='test'>";
echo "<h3>Test 4: getBlogs() Function Test</h3>";

$testBlogs = getBlogs(\$conn, [
    'show_on_home' => 1,
    'limit' => 10
]);

echo "<p>getBlogs(\$conn, ['show_on_home' => 1, 'limit' => 10]) returned: <strong>" . count(\$testBlogs) . "</strong> blogs</p>";

if (count(\$testBlogs) > 0) {
    echo "<p class='status-pass'>✓ PASS: Function returned blogs</p>";
    foreach (\$testBlogs as \$blog) {
        echo "<p>- " . htmlspecialchars(substr(\$blog['title'], 0, 50)) . "</p>";
    }
} else {
    if (\$homeBlogs > 0) {
        echo "<p class='status-fail'>✗ FAIL: Function returned 0 blogs but database has \$homeBlogs</p>";
        echo "<div class='warning'><strong>BUG ALERT:</strong> The getBlogs() function is not working correctly. This needs investigation.</div>";
    } else {
        echo "<p>ℹ INFO: No blogs to return (this is OK if no blogs are published)</p>";
    }
}

echo "</div>";

// TEST 5: Test homepage include
echo "<div class='test'>";
echo "<h3>Test 5: Simulate Homepage Include</h3>";

echo "<p>Testing what would be output to homepage...</p>";

ob_start();
require_once 'home_blog_section.php';
\$output = ob_get_clean();

if (strpos(\$output, 'featured-blogs-section') !== false) {
    echo "<p class='status-pass'>✓ PASS: Blog section HTML would be included</p>";
    if (strpos(\$output, 'blog-article') !== false) {
        echo "<p>✓ Blog articles are being rendered</p>";
    } else {
        echo "<p>⚠ Warning: Blog section included but no articles found (this is OK if no blogs marked for homepage)</p>";
    }
} else {
    echo "<p class='status-fail'>✗ FAIL: Blog section would NOT be included (no blogs match criteria)</p>";
}

echo "</div>";

// SUMMARY
echo "<div class='test' style='margin-top: 30px; border-left-color: #9b59b6;'>";
echo "<h3>📋 Summary & Action Items</h3>";

if (\$homeBlogs > 0 && strpos(\$indexContent, 'home_blog_section.php') !== false) {
    echo "<p style='color: green; font-weight: bold;'>✓ Everything looks good! Blogs should appear on homepage.</p>";
} else {
    echo "<p style='color: red; font-weight: bold;'>⚠ Blogs will NOT appear. See issues above.</p>";
    echo "<ol>";
    if (strpos(\$indexContent, 'home_blog_section.php') === false) {
        echo "<li><strong>Add blog section to index.php:</strong> Include home_blog_section.php before &lt;/main&gt;</li>";
    }
    if (\$homeBlogs === 0) {
        echo "<li><strong>Mark blogs for homepage:</strong>";
        echo "<ul>";
        echo "<li>Go to <a href='admin_blogs.php' class='btn'>Manage Blogs</a></li>";
        echo "<li>Edit a blog</li>";
        echo "<li>Check 'Show on Homepage'</li>";
        echo "<li>Make sure status is 'Published'</li>";
        echo "<li>Make sure Publish Date is today or earlier</li>";
        echo "<li>Save</li>";
        echo "</ul>";
        echo "</li>";
    }
    echo "</ol>";
}

echo "</div>";

echo "<div style='margin-top: 30px;'>";
echo "<a href='index.php' class='btn'>View Homepage</a> ";
echo "<a href='admin_blogs.php' class='btn'>Manage Blogs</a> ";
echo "<a href='admin_add_blog.php' class='btn'>Add New Blog</a>";
echo "</div>";

echo "</div></body></html>";
?>
