<?php

namespace Database\Seeders;

use App\Models\LearningMaterial;
use App\Models\SchoolClass;
use App\Models\User;
use Illuminate\Database\Seeder;

class LmsSeeder extends Seeder
{
    public function run(): void
    {
        $teacher = User::role('Teacher')->first();

        // Post a few items to the first couple of Main-campus classes that have students.
        $classes = SchoolClass::withoutGlobalScopes()->where('branch_id', 1)
            ->whereHas('students')->with('subjects')->orderBy('level')->take(3)->get();

        foreach ($classes as $class) {
            $subject = $class->subjects->first();

            LearningMaterial::firstOrCreate(
                ['branch_id' => 1, 'class_id' => $class->id, 'type' => 'homework', 'title' => 'Maths Worksheet — '.$class->name],
                ['subject_id' => $subject?->id, 'description' => 'Complete exercises 1–10 from chapter 3.',
                    'due_date' => now()->addDays(3), 'is_published' => true, 'posted_by' => $teacher?->id]
            );

            LearningMaterial::firstOrCreate(
                ['branch_id' => 1, 'class_id' => $class->id, 'type' => 'assignment', 'title' => 'Science Project — '.$class->name],
                ['subject_id' => $subject?->id, 'description' => 'Prepare a one-page report on the water cycle and submit.',
                    'due_date' => now()->addDays(7), 'is_published' => true, 'posted_by' => $teacher?->id]
            );

            LearningMaterial::firstOrCreate(
                ['branch_id' => 1, 'class_id' => $class->id, 'type' => 'notes', 'title' => 'Chapter 3 Notes — '.$class->name],
                ['subject_id' => $subject?->id, 'description' => 'Reference notes for chapter 3.',
                    'is_published' => true, 'posted_by' => $teacher?->id]
            );
        }
    }
}
