<?php

namespace Database\Seeders;

use App\Models\School;
use App\Models\SchoolClass;
use App\Models\Subject;
use App\Models\Assignment;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class TeacherSeeder extends Seeder
{
    public function run(): void
    {
        $school = School::first(); // Bungoma DEB Primary from previous seeder

        if (!$school) {
            $this->command->warn('No school found. Run UserSeeder first.');
            return;
        }

        $teacher = User::where('email', 'teacher@institutionaxis.com')->first();

        if (!$teacher) {
            $teacher = User::create([
                'name' => 'Mrs. Moraa',
                'email' => 'teacher@institutionaxis.com',
                'password' => Hash::make('password'),
                'role' => 'teacher',
                'school_id' => $school->id,
            ]);
        }

        // Create Classes
        $class7 = SchoolClass::create([
            'school_id' => $school->id,
            'name' => 'Grade 7',
            'level' => 'primary',
            'stream' => 'A',
            'capacity' => 45,
        ]);

        $class8 = SchoolClass::create([
            'school_id' => $school->id,
            'name' => 'Grade 8',
            'level' => 'primary',
            'stream' => 'B',
            'capacity' => 42,
        ]);

        // Create Subjects
        $math = Subject::create([
            'school_id' => $school->id,
            'name' => 'Mathematics',
            'code' => 'MATH007',
        ]);

        $science = Subject::create([
            'school_id' => $school->id,
            'name' => 'Science',
            'code' => 'SCI008',
        ]);

        $english = Subject::create([
            'school_id' => $school->id,
            'name' => 'English',
            'code' => 'ENG007',
        ]);

        // Link Teacher to Classes & Subjects (Many-to-Many)
        $teacher->taughtClasses()->attach($class7->id, ['subject_id' => $math->id]);
        $teacher->taughtClasses()->attach($class7->id, ['subject_id' => $science->id]);
        $teacher->taughtClasses()->attach($class8->id, ['subject_id' => $english->id]);

        // Create Sample Assignments
        Assignment::create([
            'school_class_id' => $class7->id,
            'subject_id' => $math->id,
            'teacher_id' => $teacher->id,
            'title' => 'Mathematics CAT - Grade 7',
            'description' => 'Complete questions 1-20 from page 45',
            'due_date' => now()->addDays(3),
            'status' => 'published',
        ]);

        Assignment::create([
            'school_class_id' => $class7->id,
            'subject_id' => $science->id,
            'teacher_id' => $teacher->id,
            'title' => 'Science Project submission',
            'description' => 'Prepare a model of the solar system',
            'due_date' => now()->addDays(6),
            'status' => 'published',
        ]);

        $this->command->info('TeacherSeeder completed successfully!');
    }
}