<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Module;
use App\Models\Course;
use App\Models\Level;
use App\Models\Subject;

class ModuleSeeder extends Seeder
{
    public function run(): void
    {
        $alevel   = Level::where('name', 'A Level')->first();
        $maths    = Subject::where('name', 'Mathematics')->first();

        $mathsCourse = Course::where('level_id', $alevel->id)
                             ->where('subject_id', $maths->id)
                             ->first();

        if (!$mathsCourse) {
            $this->command->warn('Maths course not found, skipping ModuleSeeder.');
            return;
        }

        $modules = [
            ['course_id' => $mathsCourse->id, 'name' => 'Pure Mathematics', 'order' => 1],
            ['course_id' => $mathsCourse->id, 'name' => 'Statistics', 'order' => 2],
        ];

        foreach ($modules as $module) {
            Module::updateOrCreate(
                ['course_id' => $module['course_id'], 'name' => $module['name']],
                $module
            );
        }
    }
}