<?php

namespace console\migrations;

use yii\db\Migration;

/**
 * Class m250801_130000_insert_sample_booking_data
 * 
 * Creates sample data for testing the booking system
 */
class m250801_130000_insert_sample_booking_data extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        // Insert sample sports
        $this->batchInsert('sports', ['name', 'description', 'duration_minutes', 'max_participants', 'price', 'color', 'status'], [
            ['Surfing', 'Learn to surf with our professional instructors', 120, 4, 75.00, '#FF6B35', 'active'],
            ['Windsurfing', 'Master the art of windsurfing', 90, 2, 85.00, '#4ECDC4', 'active'],
            ['Kitesurfing', 'Experience the thrill of kitesurfing', 150, 2, 120.00, '#45B7D1', 'active'],
            ['Stand Up Paddle', 'Relaxing stand up paddleboard lessons', 60, 6, 45.00, '#96CEB4', 'active'],
            ['Yoga on Beach', 'Peaceful yoga sessions on the beach', 75, 8, 35.00, '#FFEAA7', 'active'],
        ]);

        // Insert sample instructors
        $this->batchInsert('instructors', ['name', 'email', 'phone', 'specialization', 'bio', 'hourly_rate', 'status'], [
            ['John Smith', 'john@ionclub.com', '+1-555-0101', 'Surfing, Windsurfing', 'Professional surf instructor with 10+ years experience', 45.00, 'active'],
            ['Maria Rodriguez', 'maria@ionclub.com', '+1-555-0102', 'Kitesurfing, SUP', 'Expert kitesurfer and certified SUP instructor', 50.00, 'active'],
            ['David Chen', 'david@ionclub.com', '+1-555-0103', 'Surfing, SUP', 'Former competitive surfer turned instructor', 48.00, 'active'],
            ['Sophie Martin', 'sophie@ionclub.com', '+1-555-0104', 'Yoga, SUP', 'Certified yoga instructor specializing in beach sessions', 40.00, 'active'],
            ['Alex Thompson', 'alex@ionclub.com', '+1-555-0105', 'Windsurfing, Kitesurfing', 'International windsurfing champion', 55.00, 'active'],
        ]);

        // Get inserted IDs for sports and instructors
        $sports = $this->db->createCommand('SELECT id, name FROM sports')->queryAll();
        $instructors = $this->db->createCommand('SELECT id, name FROM instructors')->queryAll();

        // Create sport name to ID mapping
        $sportMap = [];
        foreach ($sports as $sport) {
            $sportMap[$sport['name']] = $sport['id'];
        }

        // Create instructor name to ID mapping
        $instructorMap = [];
        foreach ($instructors as $instructor) {
            $instructorMap[$instructor['name']] = $instructor['id'];
        }

        // Insert instructor-sport relationships
        $instructorSports = [
            ['John Smith', ['Surfing', 'Windsurfing']],
            ['Maria Rodriguez', ['Kitesurfing', 'Stand Up Paddle']],
            ['David Chen', ['Surfing', 'Stand Up Paddle']],
            ['Sophie Martin', ['Yoga on Beach', 'Stand Up Paddle']],
            ['Alex Thompson', ['Windsurfing', 'Kitesurfing']],
        ];

        foreach ($instructorSports as [$instructorName, $sportNames]) {
            $instructorId = $instructorMap[$instructorName];
            foreach ($sportNames as $sportName) {
                if (isset($sportMap[$sportName])) {
                    $this->insert('instructor_sports', [
                        'instructor_id' => $instructorId,
                        'sport_id' => $sportMap[$sportName],
                    ]);
                }
            }
        }

        // Insert instructor availability (all instructors available 8 AM - 6 PM, Monday-Sunday)
        $days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
        foreach ($instructors as $instructor) {
            foreach ($days as $day) {
                $this->insert('instructor_availability', [
                    'instructor_id' => $instructor['id'],
                    'day_of_week' => $day,
                    'start_time' => '08:00:00',
                    'end_time' => '18:00:00',
                    'is_available' => 1,
                ]);
            }
        }

        // Insert sample bookings for the next few days
        $today = date('Y-m-d');
        $tomorrow = date('Y-m-d', strtotime('+1 day'));
        $dayAfter = date('Y-m-d', strtotime('+2 days'));

        $sampleBookings = [
            // Today's bookings
            [
                'booking_reference' => 'BK' . date('Ymd') . '001',
                'customer_name' => 'Alice Johnson',
                'customer_email' => 'alice@example.com',
                'customer_phone' => '+1-555-1001',
                'sport_id' => $sportMap['Surfing'],
                'instructor_id' => $instructorMap['John Smith'],
                'lesson_date' => $today,
                'start_time' => '09:00:00',
                'duration_minutes' => 120,
                'participants' => 2,
                'skill_level' => 'beginner',
                'price' => 150.00,
                'status' => 'confirmed',
                'payment_status' => 'paid',
                'special_requests' => 'First time surfing, please be patient',
            ],
            [
                'booking_reference' => 'BK' . date('Ymd') . '002',
                'customer_name' => 'Bob Wilson',
                'customer_email' => 'bob@example.com',
                'customer_phone' => '+1-555-1002',
                'sport_id' => $sportMap['Kitesurfing'],
                'instructor_id' => $instructorMap['Maria Rodriguez'],
                'lesson_date' => $today,
                'start_time' => '14:00:00',
                'duration_minutes' => 150,
                'participants' => 1,
                'skill_level' => 'intermediate',
                'price' => 120.00,
                'status' => 'confirmed',
                'payment_status' => 'paid',
            ],
            
            // Tomorrow's bookings
            [
                'booking_reference' => 'BK' . date('Ymd', strtotime('+1 day')) . '001',
                'customer_name' => 'Carol Davis',
                'customer_email' => 'carol@example.com',
                'customer_phone' => '+1-555-1003',
                'sport_id' => $sportMap['Stand Up Paddle'],
                'instructor_id' => $instructorMap['David Chen'],
                'lesson_date' => $tomorrow,
                'start_time' => '10:00:00',
                'duration_minutes' => 60,
                'participants' => 3,
                'skill_level' => 'beginner',
                'price' => 135.00,
                'status' => 'pending',
                'payment_status' => 'pending',
                'special_requests' => 'Group lesson for family',
            ],
            [
                'booking_reference' => 'BK' . date('Ymd', strtotime('+1 day')) . '002',
                'customer_name' => 'Daniel Brown',
                'customer_email' => 'daniel@example.com',
                'customer_phone' => '+1-555-1004',
                'sport_id' => $sportMap['Yoga on Beach'],
                'instructor_id' => $instructorMap['Sophie Martin'],
                'lesson_date' => $tomorrow,
                'start_time' => '07:00:00',
                'duration_minutes' => 75,
                'participants' => 1,
                'skill_level' => 'intermediate',
                'price' => 35.00,
                'status' => 'confirmed',
                'payment_status' => 'paid',
            ],
            
            // Day after tomorrow's bookings
            [
                'booking_reference' => 'BK' . date('Ymd', strtotime('+2 days')) . '001',
                'customer_name' => 'Eva Martinez',
                'customer_email' => 'eva@example.com',
                'customer_phone' => '+1-555-1005',
                'sport_id' => $sportMap['Windsurfing'],
                'instructor_id' => $instructorMap['Alex Thompson'],
                'lesson_date' => $dayAfter,
                'start_time' => '15:00:00',
                'duration_minutes' => 90,
                'participants' => 2,
                'skill_level' => 'advanced',
                'price' => 170.00,
                'status' => 'confirmed',
                'payment_status' => 'paid',
                'special_requests' => 'Advanced techniques focus',
            ],
        ];

        foreach ($sampleBookings as $booking) {
            $this->insert('bookings', $booking);
        }

        echo "Sample data inserted successfully!\n";
        echo "Created:\n";
        echo "- 5 sports\n";
        echo "- 5 instructors\n";
        echo "- Instructor availability schedules\n";
        echo "- 5 sample bookings\n";
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        // Delete in reverse order to respect foreign key constraints
        $this->delete('bookings');
        $this->delete('instructor_availability');
        $this->delete('instructor_sports');
        $this->delete('instructors');
        $this->delete('sports');
        
        echo "Sample data removed successfully!\n";
    }
}
