<?php

/**
 * Update database pages to consolidate legal pages
 */

require_once __DIR__ . '/db_config.php';

// Delete the old individual policy pages
$delete_pages = ['privacy-policy', 'cookie-policy', 'terms-conditions'];
foreach ($delete_pages as $slug) {
    $stmt = $conn->prepare("DELETE FROM pages WHERE slug = ?");
    $stmt->bind_param('s', $slug);
    $stmt->execute();
    $stmt->close();
    echo "✓ Deleted: $slug<br>";
}

// Check if legal-policies page exists
$check = $conn->prepare("SELECT id FROM pages WHERE slug = ?");
$slug = 'legal-policies';
$check->bind_param('s', $slug);
$check->execute();
$result = $check->get_result();

if ($result->num_rows == 0) {
    // Insert the new unified legal policies page
    $insert = $conn->prepare("INSERT INTO pages (title, slug, url, is_featured, menu_order) VALUES (?, ?, ?, ?, ?)");
    $title = 'Legal & Policies';
    $url = 'legal_policies.php';
    $featured = 0;
    $order = 9;
    $insert->bind_param('sssii', $title, $slug, $url, $featured, $order);
    $insert->execute();
    echo "✓ Created: Legal & Policies page<br>";
    $insert->close();
}
$check->close();

// Show all pages
echo "<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();
?>
