<?php
/**
 * Insert missing pages into pages table
 */

require_once __DIR__ . '/db_config.php';

$new_pages = [
    ['title' => 'Awards', 'slug' => 'awards', 'url' => 'awards.php', 'is_featured' => 0, 'menu_order' => 5],
    ['title' => 'Brands & Catalogs', 'slug' => 'brands-catalogs', 'url' => 'brands_catalogs.php', 'is_featured' => 0, 'menu_order' => 6],
    ['title' => 'Custom Kits & Services', 'slug' => 'kits-services', 'url' => 'kits_services.php', 'is_featured' => 0, 'menu_order' => 7],
    ['title' => 'Privacy Policy', 'slug' => 'privacy-policy', 'url' => 'privacy_policy.php', 'is_featured' => 0, 'menu_order' => 9],
    ['title' => 'Cookie Policy', 'slug' => 'cookie-policy', 'url' => 'cookie_policy.php', 'is_featured' => 0, 'menu_order' => 10],
    ['title' => 'Terms & Conditions', 'slug' => 'terms-conditions', 'url' => 'terms_conditions.php', 'is_featured' => 0, 'menu_order' => 11],
];

$inserted = 0;
$skipped = 0;

foreach ($new_pages as $page) {
    // Check if page already exists
    $check = $conn->prepare("SELECT id FROM pages WHERE slug = ?");
    $check->bind_param('s', $page['slug']);
    $check->execute();
    $check_result = $check->get_result();
    
    if ($check_result->num_rows == 0) {
        // Page doesn't exist, insert it
        $stmt = $conn->prepare("
            INSERT INTO pages (title, slug, url, is_featured, menu_order) 
            VALUES (?, ?, ?, ?, ?)
        ");
        $stmt->bind_param(
            'sssii',
            $page['title'],
            $page['slug'],
            $page['url'],
            $page['is_featured'],
            $page['menu_order']
        );
        
        if ($stmt->execute()) {
            $inserted++;
            echo "✓ Inserted: " . $page['title'] . "<br>";
        } else {
            echo "✗ Error inserting " . $page['title'] . ": " . $stmt->error . "<br>";
        }
        $stmt->close();
    } else {
        $skipped++;
        echo "• Skipped (already exists): " . $page['title'] . "<br>";
    }
    $check->close();
}

echo "<br><strong>Summary:</strong><br>";
echo "Inserted: " . $inserted . " pages<br>";
echo "Skipped: " . $skipped . " pages<br>";

// Show all pages now
echo "<br><br><strong>All pages in database:</strong><br>";
$result = $conn->query("SELECT * FROM pages ORDER BY menu_order ASC");
echo "<table border='1' cellpadding='10' style='margin-top: 20px;'>";
echo "<tr><th>ID</th><th>Title</th><th>Slug</th><th>URL</th><th>Featured</th><th>Order</th></tr>";

while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['slug'] . "</td>";
    echo "<td>" . $row['url'] . "</td>";
    echo "<td>" . ($row['is_featured'] ? "YES" : "NO") . "</td>";
    echo "<td>" . $row['menu_order'] . "</td>";
    echo "</tr>";
}
echo "</table>";

$conn->close();
?>
