<?php
require_once __DIR__ . '/config.php';

function migrate() {
    $pdo = db();
    
    // 1. Create tables
    try {
        $sql = file_get_contents(__DIR__ . '/schema.sql');
        $pdo->exec($sql);
        echo "Database schema created successfully.\n";
    } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
    }

    // 2. Insert data (idempotently)
    $experiences_data = [
        [
            'slug' => 'guided-ski-lessons',
            'title' => 'Guided Ski & Snowboard Lessons',
            'image' => 'https://images.pexels.com/photos/167699/pexels-photo-167699.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2',
            'description' => 'Whether you\'re a first-timer or a seasoned pro, our certified instructors are here to help you sharpen your skills and conquer the mountain with confidence. We offer private and group lessons for all ages and abilities.',
            'features' => json_encode([
                'Duration' => '2 hours, Half-day, or Full-day',
                'Skill Level' => 'Beginner to Expert',
                'Group Size' => '1-6 people',
                'Includes' => 'Lift ticket & rental discounts'
            ])
        ],
        [
            'slug' => 'enchanted-ice-skating',
            'title' => 'Enchanted Ice Skating',
            'image' => 'https://images.pexels.com/photos/714258/pexels-photo-714258.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2',
            'description' => 'Glide across our scenic outdoor rink, surrounded by magical winter lights and festive music. A perfect activity for families, couples, and friends. Skate rentals available.',
            'features' => json_encode([
                'Duration' => 'All-day pass',
                'Skill Level' => 'All levels',
                'Group Size' => 'N/A',
                'Includes' => 'Skate rentals available for a fee'
            ])
        ],
        [
            'slug' => 'cozy-winter-markets',
            'title' => 'Cozy Winter Markets',
            'image' => 'https://images.pexels.com/photos/1525612/pexels-photo-1525612.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2',
            'description' => 'Explore our charming village market, filled with handcrafted gifts from local artisans, delicious seasonal treats, and warm spiced beverages. A festive experience for all ages.',
            'features' => json_encode([
                'Duration' => 'Weekends in December',
                'Skill Level' => 'N/A',
                'Group Size' => 'N/A',
                'Includes' => 'Unique gifts and festive food'
            ])
        ]
    ];

    $stmt = $pdo->prepare("INSERT INTO experiences (slug, title, image, description, features) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE title=VALUES(title), image=VALUES(image), description=VALUES(description), features=VALUES(features)");

    try {
        foreach ($experiences_data as $exp) {
            $stmt->execute(array_values($exp));
        }
        echo "Experience data inserted/updated successfully.\n";
    } catch (PDOException $e) {
        die("DB ERROR: ". $e->getMessage());
    }
}

migrate();

