<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\ClassCourse;
use App\Models\ClassSchedule;
use App\Models\ClassScheduleException;
use App\Models\Coach;
use App\Models\Venue;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class ClassScheduleDemoSeeder extends Seeder
{
    public function run(): void
    {
        if (ClassSchedule::query()->exists()) {
            return;
        }

        $venue = Venue::query()->where('is_active', true)->orderBy('sort_order')->first();
        if (! $venue instanceof Venue) {
            return;
        }

        $courses = ClassCourse::query()
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->limit(6)
            ->get();

        if ($courses->isEmpty()) {
            return;
        }

        $coach = Coach::query()->where('is_active', true)->orderBy('sort_order')->first();

        $timeSlots = [
            ['08:30', '09:30'],
            ['09:35', '10:35'],
            ['19:30', '20:30'],
        ];

        $dayOrder = [1, 2, 3, 4, 5, 6, 0];

        $createdSchedules = [];

        foreach (array_slice($dayOrder, 0, 6) as $dayIndex => $dayOfWeek) {
            foreach (array_slice($timeSlots, 0, 2) as $slotIndex => [$start, $end]) {
                $course = $courses[($dayIndex + $slotIndex) % $courses->count()];

                $createdSchedules[] = ClassSchedule::create([
                    'venue_id' => $venue->id,
                    'course_id' => $course->id,
                    'day_of_week' => $dayOfWeek,
                    'start_time' => $start,
                    'end_time' => $end,
                    'classroom' => '有氧團體教室',
                    'class_title' => $course->name,
                    'coach_id' => $coach?->id,
                    'coach_display_name' => $coach?->name,
                    'location' => '有氧教室',
                    'level' => $course->level,
                    'note' => null,
                    'sort_order' => ($dayIndex + 1) * 10 + $slotIndex,
                    'is_active' => true,
                ]);
            }
        }

        if (ClassScheduleException::query()->exists()) {
            return;
        }

        $weekStart = Carbon::now()->startOfWeek(Carbon::MONDAY);
        $nextMonday = $weekStart->copy();
        $nextTuesday = $weekStart->copy()->addDay();

        $firstSchedule = $createdSchedules[0] ?? null;
        $secondSchedule = $createdSchedules[1] ?? null;

        if ($firstSchedule instanceof ClassSchedule) {
            ClassScheduleException::create([
                'venue_id' => $venue->id,
                'schedule_id' => $firstSchedule->id,
                'date' => $nextMonday->toDateString(),
                'action' => 'cancel',
                'course_id' => $firstSchedule->course_id,
                'start_time' => null,
                'end_time' => null,
                'classroom' => $firstSchedule->classroom,
                'class_title' => $firstSchedule->class_title,
                'coach_id' => $firstSchedule->coach_id,
                'coach_display_name' => $firstSchedule->coach_display_name,
                'location' => $firstSchedule->location,
                'level' => $firstSchedule->level,
                'note' => '示範：停課',
                'sort_order' => 10,
                'is_active' => true,
            ]);
        }

        if ($secondSchedule instanceof ClassSchedule) {
            $altCourse = $courses->last();

            ClassScheduleException::create([
                'venue_id' => $venue->id,
                'schedule_id' => $secondSchedule->id,
                'date' => $nextTuesday->toDateString(),
                'action' => 'override',
                'course_id' => $altCourse->id,
                'start_time' => $secondSchedule->start_time,
                'end_time' => $secondSchedule->end_time,
                'classroom' => $secondSchedule->classroom,
                'class_title' => $altCourse->name,
                'coach_id' => $coach?->id,
                'coach_display_name' => $coach?->name,
                'location' => $secondSchedule->location,
                'level' => $altCourse->level,
                'note' => '示範：異動（改課程）',
                'sort_order' => 20,
                'is_active' => true,
            ]);
        }

        $addCourse = $courses->first();
        ClassScheduleException::create([
            'venue_id' => $venue->id,
            'schedule_id' => null,
            'date' => $nextMonday->toDateString(),
            'action' => 'add',
            'course_id' => $addCourse->id,
            'start_time' => '18:30',
            'end_time' => '19:20',
            'classroom' => '飛輪教室',
            'class_title' => $addCourse->name,
            'coach_id' => $coach?->id,
            'coach_display_name' => $coach?->name,
            'location' => '飛輪教室',
            'level' => $addCourse->level,
            'note' => '示範：加開課程',
            'sort_order' => 30,
            'is_active' => true,
        ]);
    }
}
