<?php

/**
 * Database Migration Script
 * This file contains all database table creation scripts
 * Each table is checked for existence before creation
 */

require_once __DIR__ . '/../vendor/autoload.php';

try {
    $db = db();
    
    echo "Starting database migration...\n";
    
    // Migration: Create settings table
    $settingsTableSQL = "
    CREATE TABLE IF NOT EXISTS settings (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        grupa VARCHAR(255) NOT NULL,
        name VARCHAR(255) NOT NULL,
        value TEXT
    )";
    
    $db->execute($settingsTableSQL);
    echo " Settings table checked/created\n";
    
    // Create index for settings table
    $settingsIndexSQL = "CREATE INDEX IF NOT EXISTS idx_settings_grupa_name ON settings(grupa, name)";
    $db->execute($settingsIndexSQL);
    echo " Settings table index checked/created\n";
    
    
    // Insert default settings if they don't exist
    $defaultSettings = [
        // Content settings
        ['grupa' => 'content', 'name' => 'title', 'value' => 'Welcome to Amazing Casino - Your Ultimate Gaming Destination'],
        ['grupa' => 'content', 'name' => 'top_text', 'value' => '<h2>Best Online Casino Experience</h2><p>Discover the most exciting online casino games with amazing bonuses and instant payouts!</p>'],
        ['grupa' => 'content', 'name' => 'bottom_text', 'value' => '<p>Join thousands of players who trust our platform for secure gaming. Play responsibly and enjoy the thrill of winning big!</p>'],
        ['grupa' => 'content', 'name' => 'author-name', 'value' => 'John Smith'],
        ['grupa' => 'content', 'name' => 'modified-date', 'value' => '2024-01-15'],
        ['grupa' => 'content', 'name' => 'login', 'value' => 'Login Now'],
        ['grupa' => 'content', 'name' => 'password', 'value' => 'Reset Password'],
        ['grupa' => 'content', 'name' => 'faq-title', 'value' => 'Frequently Asked Questions'],
        ['grupa' => 'content', 'name' => 'footer__text', 'value' => 'Your trusted casino partner since 2020. Licensed and regulated for your safety.'],
        ['grupa' => 'content', 'name' => 'copyright', 'value' => '© 2024 Amazing Casino. All rights reserved.'],
        ['grupa' => 'content', 'name' => 'review-count', 'value' => '1250'],
        ['grupa' => 'content', 'name' => 'listing', 'value' => 'Top Rated Casinos'],
        ['grupa' => 'content', 'name' => 'content_btn', 'value' => 'Play Now'],
        ['grupa' => 'content', 'name' => 'hide-btns', 'value' => '0'],
        ['grupa' => 'content', 'name' => 'header-logo', 'value' => '{"src":"uploads/logo.png","title":"Amazing Casino Logo","alt":"Amazing Casino - Best Online Gaming"}'],
        ['grupa' => 'content', 'name' => 'banner-logo', 'value' => '{"src":"uploads/banner.jpg","title":"Welcome Bonus Banner","alt":"Get 200% Welcome Bonus","width":"800","height":"400"}'],
        ['grupa' => 'content', 'name' => 'footer-logo', 'value' => '{"src":"uploads/footer-logo.png","title":"Footer Logo","alt":"Amazing Casino Footer"}'],
        ['grupa' => 'content', 'name' => 'favicon', 'value' => 'uploads/favicon.ico'],
        ['grupa' => 'content', 'name' => 'author-img', 'value' => 'uploads/author.jpg'],
        ['grupa' => 'content', 'name' => 'hash', 'value' => 'ac123main'],
        ['grupa' => 'content', 'name' => 'hash-1', 'value' => 'ac123promo'],
        ['grupa' => 'content', 'name' => 'hash-2', 'value' => 'ac123bonus'],
        ['grupa' => 'content', 'name' => 'hash-3', 'value' => 'ac123games'],
        ['grupa' => 'content', 'name' => 'hash-4', 'value' => 'ac123vip'],
        ['grupa' => 'content', 'name' => 'hash-5', 'value' => 'ac123support'],
        ['grupa' => 'content', 'name' => 'casino_list', 'value' => '[{"heading":"Royal Casino","text":"200% Welcome Bonus + 50 Free Spins","button":"Claim Bonus","rating":"10"},{"heading":"Golden Palace","text":"100% Match Bonus up to $500","button":"Play Now","rating":"9.5"},{"heading":"Diamond Club","text":"$1000 Welcome Package + VIP Treatment","button":"Join VIP","rating":"9"}]'],
        ['grupa' => 'content', 'name' => 'faq', 'value' => '[{"question":"How do I create an account?","answer":"Click the Register button and fill in your details. Verification usually takes 5-10 minutes."},{"question":"What payment methods do you accept?","answer":"We accept credit cards, e-wallets, bank transfers, and cryptocurrencies."},{"question":"How long do withdrawals take?","answer":"Most withdrawals are processed within 24 hours."}]'],
        ['grupa' => 'content', 'name' => 'menu', 'value' => '[{"title":"Home","anchor":"#home"},{"title":"Games","anchor":"#games"},{"title":"Bonuses","anchor":"#bonuses"},{"title":"Reviews","anchor":"#reviews"},{"title":"Contact","anchor":"#contact"}]'],
        
        // Style settings
        ['grupa' => 'styles', 'name' => 'main_color', 'value' => '#1a1a2e'],
        ['grupa' => 'styles', 'name' => 'secondary_color', 'value' => '#16213e'],
        ['grupa' => 'styles', 'name' => 'accent_color', 'value' => '#e94560'],
        ['grupa' => 'styles', 'name' => 'font_color', 'value' => '#ffffff'],
        ['grupa' => 'styles', 'name' => 'hover_link_color', 'value' => '#f39c12'],
        ['grupa' => 'styles', 'name' => 'main-fz', 'value' => 'Arial, sans-serif'],
        ['grupa' => 'styles', 'name' => 'footer_color', 'value' => '#0f0f23'],
        ['grupa' => 'styles', 'name' => 'link_color', 'value' => '#3498db'],
        ['grupa' => 'styles', 'name' => 'header_color', 'value' => '#2c2c54'],
        ['grupa' => 'styles', 'name' => 'login_btn_color', 'value' => '#27ae60'],
        ['grupa' => 'styles', 'name' => 'pass_btn_color', 'value' => '#e74c3c'],
        ['grupa' => 'styles', 'name' => 'table', 'value' => '#ecf0f1'],
        ['grupa' => 'styles', 'name' => 'caption', 'value' => '#bdc3c7'],
        ['grupa' => 'styles', 'name' => 'faq-title-font-color', 'value' => '#2c3e50'],
        ['grupa' => 'styles', 'name' => 'faq-title-hover-font-color', 'value' => '#e94560'],
        ['grupa' => 'styles', 'name' => 'burger-color', 'value' => '#ffffff'],

        // SEO settings
        ['grupa' => 'seo', 'name' => 'title', 'value' => 'Amazing Casino - Best Online Gaming Experience 2024'],
        ['grupa' => 'seo', 'name' => 'description', 'value' => 'Join Amazing Casino for the ultimate online gaming experience. Enjoy 200+ games, generous bonuses, and secure payments. Licensed and trusted worldwide.'],
        ['grupa' => 'seo', 'name' => 'keywords', 'value' => 'online casino, casino games, slots, blackjack, poker, roulette, bonuses, secure gaming'],
        ['grupa' => 'seo', 'name' => 'canonical', 'value' => 'https://amazing-casino.com'],
        ['grupa' => 'seo', 'name' => 'og-type', 'value' => 'website'],
        ['grupa' => 'seo', 'name' => 'og-title', 'value' => 'Amazing Casino - Win Big with Our Casino Games'],
        ['grupa' => 'seo', 'name' => 'og-description', 'value' => 'Experience the thrill of premium casino games with Amazing Casino. Get your 200% welcome bonus today!'],
        ['grupa' => 'seo', 'name' => 'og-locale', 'value' => 'en_US'],
        ['grupa' => 'seo', 'name' => 'og-image', 'value' => 'uploads/og-image.jpg'],
        ['grupa' => 'seo', 'name' => 'lang', 'value' => 'en'],
        ['grupa' => 'seo', 'name' => 'lang-page', 'value' => 'en-US'],
        ['grupa' => 'seo', 'name' => 'hreflang', 'value' => '[{"hreflang":"en","href":"https://amazing-casino.com/en/"},{"hreflang":"es","href":"https://amazing-casino.com/es/"},{"hreflang":"fr","href":"https://amazing-casino.com/fr/"},{"hreflang":"x-default","href":"https://amazing-casino.com/"}]'],
    ];
    
    foreach ($defaultSettings as $setting) {
        $exists = $db->fetchOne(
            "SELECT id FROM settings WHERE grupa = ? AND name = ?",
            [$setting['grupa'], $setting['name']]
        );
        
        if (!$exists) {
            $db->execute(
                "INSERT INTO settings (grupa, name, value) VALUES (?, ?, ?)",
                [$setting['grupa'], $setting['name'], $setting['value']]
            );
            echo "✓ Default setting {$setting['grupa']}.{$setting['name']} created\n";
        }
    }
    
    echo "Migration completed successfully!\n";
    
} catch (Exception $e) {
    echo " Migration failed: " . $e->getMessage() . "\n";
    exit(1);
}