<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\School;
use App\Models\AcademicYear;
use App\Models\Term;
use App\Models\SchoolClass;
use App\Models\Stream;
use App\Models\Department;
use App\Models\Subject;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Roles
        $roles = ['super-admin', 'admin', 'teacher', 'student', 'parent'];
        foreach ($roles as $role) {
            Role::firstOrCreate(['name' => $role, 'guard_name' => 'web']);
        }

        // School
        $school = School::firstOrCreate(['code' => 'SCH001'], [
            'name'    => 'EduSmart High School',
            'email'   => 'info@edusmart.ac.ke',
            'phone'   => '+254700000000',
            'address' => 'Nairobi, Kenya',
            'county'  => 'Nairobi',
            'type'    => 'secondary',
        ]);

        // Academic Year & Terms
        $year = AcademicYear::firstOrCreate(['school_id' => $school->id, 'name' => '2025'], [
            'start_date' => '2025-01-06',
            'end_date'   => '2025-11-28',
            'is_current' => true,
        ]);

        $term = Term::firstOrCreate(['academic_year_id' => $year->id, 'name' => 'Term 1'], [
            'start_date' => '2025-01-06',
            'end_date'   => '2025-04-04',
            'is_current' => true,
        ]);

        // Classes & Streams
        $classNames = ['Form 1', 'Form 2', 'Form 3', 'Form 4'];
        $streamNames = ['North', 'South', 'East'];

        foreach ($classNames as $i => $className) {
            $class = SchoolClass::firstOrCreate(['school_id' => $school->id, 'name' => $className], ['level' => $i + 1]);
            foreach ($streamNames as $streamName) {
                Stream::firstOrCreate(['class_id' => $class->id, 'name' => $streamName]);
            }
        }

        // Departments & Subjects
        $depts = [
            'Sciences'    => ['Mathematics', 'Physics', 'Chemistry', 'Biology'],
            'Languages'   => ['English', 'Kiswahili'],
            'Humanities'  => ['History', 'Geography', 'CRE'],
            'Technical'   => ['Computer Studies', 'Business Studies'],
        ];

        foreach ($depts as $deptName => $subjects) {
            $dept = Department::firstOrCreate(['school_id' => $school->id, 'name' => $deptName]);
            foreach ($subjects as $subjectName) {
                Subject::firstOrCreate(['school_id' => $school->id, 'name' => $subjectName], [
                    'department_id' => $dept->id,
                    'code'          => strtoupper(substr($subjectName, 0, 4)),
                    'is_compulsory' => in_array($subjectName, ['Mathematics', 'English', 'Kiswahili']),
                ]);
            }
        }

        // Super Admin
        $superAdmin = User::firstOrCreate(['email' => 'superadmin@edusmart.ac.ke'], [
            'name'      => 'Super Admin',
            'password'  => Hash::make('Admin@1234'),
            'school_id' => $school->id,
            'is_active' => true,
        ]);
        $superAdmin->assignRole('super-admin');

        // School Admin
        $admin = User::firstOrCreate(['email' => 'admin@edusmart.ac.ke'], [
            'name'      => 'School Admin',
            'password'  => Hash::make('Admin@1234'),
            'school_id' => $school->id,
            'is_active' => true,
        ]);
        $admin->assignRole('admin');

        $this->command->info('✅ Seeding complete. Login: superadmin@edusmart.ac.ke / Admin@1234');
    }
}
