<?php
/**
 * Create or verify Pages table in database
 * Run this once during setup
 */

require_once __DIR__ . '/db_config.php';

$tables_created = false;

// Check if pages table exists
$check_table = $conn->query("SHOW TABLES LIKE 'pages'");
if ($check_table->num_rows == 0) {
    // Create pages table
    $sql = "CREATE TABLE IF NOT EXISTS pages (
        id INT AUTO_INCREMENT PRIMARY KEY,
        title VARCHAR(255) NOT NULL UNIQUE,
        slug VARCHAR(255) NOT NULL UNIQUE,
        url VARCHAR(500) NOT NULL,
        is_featured BOOLEAN DEFAULT 0,
        menu_order INT DEFAULT 999,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
    
    if ($conn->query($sql) === TRUE) {
        $tables_created = true;
        error_log('Pages table created successfully');
        
        // Insert default pages
        $default_pages = [
            ['title' => 'Home', 'slug' => 'home', 'url' => 'index.php', 'is_featured' => 1, 'menu_order' => 1],
            ['title' => 'Shop', 'slug' => 'shop', 'url' => 'shop.php', 'is_featured' => 1, 'menu_order' => 2],
            ['title' => 'Blogs', 'slug' => 'blogs', 'url' => 'blogs.php', 'is_featured' => 1, 'menu_order' => 3],
            ['title' => 'About Us', 'slug' => 'about', 'url' => 'about.php', 'is_featured' => 0, 'menu_order' => 4],
            ['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' => 'Contact', 'slug' => 'contact', 'url' => 'contact.php', 'is_featured' => 0, 'menu_order' => 8],
            ['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],
        ];
        
        foreach ($default_pages as $page) {
            $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']
            );
            $stmt->execute();
            $stmt->close();
        }
        
        echo "✓ Pages table created and default pages inserted";
    } else {
        echo "✗ Error creating pages table: " . $conn->error;
    }
} else {
    echo "✓ Pages table already exists";
}

$conn->close();
?>
