<?php

namespace Database\Seeders;

use App\Enums\ClassMethod;
use App\Models\AcademicTerm;
use App\Models\Course;
use App\Models\CourseClass;
use App\Models\Lecturer;
use Illuminate\Database\Seeder;

class CourseClassSeeder extends Seeder
{
    public function run(): void
    {
        $academicTermId = AcademicTerm::where('is_active', true)->value('id')
            ?? AcademicTerm::query()->value('id');

        if (! $academicTermId) {
            return;
        }

        Course::all()->each(function (Course $course) use ($academicTermId) {
            $lecturer = Lecturer::whereHas('departments', fn ($q) => $q->where('departments.id', $course->department_id))
                ->inRandomOrder()
                ->first();

            if (! $lecturer) {
                return;
            }

            CourseClass::create([
                'course_id' => $course->id,
                'lecturer_id' => $lecturer->id,
                'academic_term_id' => $academicTermId,
                'method' => fake()->randomElement(ClassMethod::values()),
            ]);
        });
    }
}
