<?php

namespace Database\Seeders;

use App\Models\Department;
use App\Models\Programme;
use App\Models\ProgrammeRequirement;
use App\Models\School;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

/**
 * King Ceasor University's real academic structure — three schools and the
 * programmes published on kcu.ac.ug.
 *
 * Tuition figures are indicative placeholders: KCU does not publish fees on
 * the public site, so they are seeded as realistic Ugandan private-university
 * amounts for administrators to correct in Admin > Programmes.
 */
class AcademicStructureSeeder extends Seeder
{
    public function run(): void
    {
        foreach ($this->schools() as $index => $data) {
            $programmes = $data['programmes'];
            $departments = $data['departments'];
            unset($data['programmes'], $data['departments']);

            $school = School::updateOrCreate(
                ['slug' => Str::slug($data['name'])],
                $data + ['sort_order' => $index + 1, 'status' => 'active'],
            );

            $departmentIds = [];

            foreach ($departments as $order => $department) {
                $record = Department::updateOrCreate(
                    ['slug' => Str::slug($department)],
                    [
                        'school_id' => $school->id,
                        'name' => $department,
                        'status' => 'active',
                        'sort_order' => $order + 1,
                    ],
                );

                $departmentIds[$department] = $record->id;
            }

            foreach ($programmes as $order => $programme) {
                $requirements = $programme['requirements'] ?? [];
                $department = $programme['department'] ?? null;
                unset($programme['requirements'], $programme['department']);

                $record = Programme::updateOrCreate(
                    ['code' => $programme['code']],
                    $programme + [
                        'school_id' => $school->id,
                        'department_id' => $departmentIds[$department] ?? null,
                        'slug' => Str::slug($programme['name']),
                        'currency' => 'UGX',
                        'status' => 'active',
                        'sort_order' => $order + 1,
                    ],
                );

                $record->requirements()->delete();

                foreach ($requirements as $position => $requirement) {
                    $record->requirements()->create($requirement + [
                        'sort_order' => $position + 1,
                        'is_required' => $requirement['is_required'] ?? true,
                    ]);
                }
            }
        }

        $this->command?->info(sprintf(
            'Seeded %d schools, %d departments and %d programmes.',
            School::count(),
            Department::count(),
            Programme::count(),
        ));
    }

    protected function schools(): array
    {
        return [
            [
                'name' => 'School of Medicine, Health & Life Sciences',
                'code' => 'SMHLS',
                'description' => 'Training clinicians, nurses and laboratory scientists for Uganda and beyond.',
                'overview' => 'The School of Medicine, Health & Life Sciences delivers KCU\'s flagship Bachelor of Medicine and Bachelor of Surgery programme, which has achieved recognition through ECFMG pathways, opening international opportunities for our graduates. Clinical training is delivered in partnership with Mulago and Kiruddu National Referral Hospitals.',
                'departments' => ['Department of Clinical Medicine', 'Department of Nursing Sciences', 'Department of Biomedical Sciences'],
                'programmes' => [
                    [
                        'name' => 'Bachelor of Medicine and Bachelor of Surgery',
                        'code' => 'MBChB',
                        'award' => 'Bachelor of Medicine and Bachelor of Surgery (MBChB)',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Clinical Medicine',
                        'description' => 'A five-year professional medical degree with clinical rotations at national referral hospitals.',
                        'overview' => 'KCU\'s MBChB combines rigorous biomedical science with early and sustained clinical exposure. Students rotate through internal medicine, surgery, paediatrics, obstetrics and gynaecology, and community health. The programme has achieved recognition through ECFMG pathways, expanding the international opportunities available to graduates.',
                        'career_opportunities' => 'Medical officer, hospital clinician, public health practitioner, medical researcher, and entry into postgraduate specialisation in Uganda or abroad.',
                        'duration' => 5,
                        'duration_unit' => 'years',
                        'study_modes' => ['day'],
                        'campuses' => ['Mulago Campus', 'Bunga Campus'],
                        'tuition_amount' => 7500000,
                        'application_fee' => 100000,
                        'is_featured' => true,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE in Biology and Chemistry',
                                'minimum_points' => 10,
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'Biology at principal level, minimum grade C',
                                'required_subject' => 'Biology',
                                'minimum_grade' => 'C',
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'Chemistry at principal level, minimum grade C',
                                'required_subject' => 'Chemistry',
                                'minimum_grade' => 'C',
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Advanced Certificate of Education or recognised equivalent',
                                'required_qualification' => 'uace',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Bachelor of Science in Nursing',
                        'code' => 'BSN',
                        'award' => 'Bachelor of Science in Nursing',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Nursing Sciences',
                        'description' => 'Professional nursing education combining clinical practice with leadership and research.',
                        'overview' => 'The Bachelor of Science in Nursing prepares graduates for registration as professional nurses, with supervised placements across medical, surgical, maternal and community settings.',
                        'career_opportunities' => 'Registered nurse, nurse educator, clinical supervisor, public health nurse, nursing research and health administration.',
                        'duration' => 4,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus', 'Mulago Campus'],
                        'tuition_amount' => 3200000,
                        'is_featured' => true,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE in science subjects',
                                'minimum_points' => 8,
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'Biology at principal level, minimum grade D',
                                'required_subject' => 'Biology',
                                'minimum_grade' => 'D',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Diploma in Clinical Medicine and Community Health',
                        'code' => 'DCMCH',
                        'award' => 'Diploma in Clinical Medicine and Community Health',
                        'level' => Programme::LEVEL_DIPLOMA,
                        'department' => 'Department of Clinical Medicine',
                        'description' => 'Trains clinical officers to diagnose, treat and manage common conditions in community settings.',
                        'overview' => 'A practice-led diploma producing clinical officers equipped to run health centres, lead outreach programmes and provide frontline care across Uganda\'s health system.',
                        'career_opportunities' => 'Clinical officer, health centre in-charge, community health programme officer, and progression to the MBChB.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day'],
                        'campuses' => ['Mulago Campus'],
                        'tuition_amount' => 2200000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'UCE credit in Biology',
                                'required_subject' => 'Biology',
                                'minimum_grade' => 'C6',
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Certificate of Education with credits in relevant subjects',
                                'required_qualification' => 'uce',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Diploma in Medical Laboratory Technology',
                        'code' => 'DMLT',
                        'award' => 'Diploma in Medical Laboratory Technology',
                        'level' => Programme::LEVEL_DIPLOMA,
                        'department' => 'Department of Biomedical Sciences',
                        'description' => 'Laboratory diagnostics, quality assurance and biomedical analysis.',
                        'overview' => 'Students gain hands-on competence in haematology, microbiology, parasitology, clinical chemistry and blood transfusion science within accredited laboratories.',
                        'career_opportunities' => 'Laboratory technologist, diagnostic laboratory supervisor, research laboratory technician, quality assurance officer.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 2000000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'UCE credit in Biology and Chemistry',
                                'required_subject' => 'Chemistry',
                                'minimum_grade' => 'C6',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Higher Education Certificate in Biological Sciences',
                        'code' => 'HEC-BIO',
                        'award' => 'Higher Education Certificate',
                        'level' => Programme::LEVEL_CERTIFICATE,
                        'department' => 'Department of Biomedical Sciences',
                        'description' => 'A one-year bridging certificate into health and life science degrees.',
                        'overview' => 'The Higher Education Certificate gives candidates who do not meet direct-entry requirements a recognised route into KCU\'s biological and health science degrees.',
                        'career_opportunities' => 'Progression to degree study in medicine, nursing, and the biological sciences.',
                        'duration' => 1,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 1400000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Certificate of Education',
                                'required_qualification' => 'uce',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'name' => 'School of Law & Management Sciences',
                'code' => 'SLMS',
                'description' => 'Law, business and economics grounded in ethical practice and enterprise.',
                'overview' => 'The School of Law & Management Sciences develops advocates, analysts and entrepreneurs. Teaching pairs doctrinal grounding with moot courts, clinics, case work and enterprise projects so graduates arrive in practice ready to contribute.',
                'departments' => ['Department of Law', 'Department of Business & Management', 'Department of Economics'],
                'programmes' => [
                    [
                        'name' => 'Bachelor of Laws',
                        'code' => 'LLB',
                        'award' => 'Bachelor of Laws (LLB)',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Law',
                        'description' => 'A four-year professional law degree preparing graduates for the Bar Course and legal practice.',
                        'overview' => 'The LLB covers constitutional, criminal, commercial, land and international law, supported by moot court advocacy, legal clinics and research writing. Graduates proceed to the Law Development Centre for the Bar Course.',
                        'career_opportunities' => 'Advocate, state attorney, legal officer, corporate counsel, compliance officer, judicial service, policy and human rights work.',
                        'duration' => 4,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 3000000,
                        'is_featured' => true,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE in arts or humanities subjects',
                                'minimum_points' => 10,
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'UCE credit in English Language',
                                'required_subject' => 'English',
                                'minimum_grade' => 'C6',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Bachelor of Business Administration',
                        'code' => 'BBA',
                        'award' => 'Bachelor of Business Administration',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Business & Management',
                        'description' => 'Management, accounting, marketing and entrepreneurship for a competitive economy.',
                        'overview' => 'The BBA builds analytical and leadership capability across accounting, finance, marketing, human resources and operations, with specialisation options in the final year and an industry internship.',
                        'career_opportunities' => 'Business analyst, accountant, marketing officer, human resource officer, operations manager, entrepreneur.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening', 'weekend'],
                        'campuses' => ['Bunga Campus', 'Namugongo Campus'],
                        'tuition_amount' => 2400000,
                        'is_featured' => true,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE',
                                'minimum_points' => 8,
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'UCE credit in Mathematics',
                                'required_subject' => 'Mathematics',
                                'minimum_grade' => 'C6',
                                'is_required' => false,
                            ],
                        ],
                    ],
                    [
                        'name' => 'Bachelor of Science in Applied Economics',
                        'code' => 'BSAE',
                        'award' => 'Bachelor of Science in Applied Economics',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Economics',
                        'description' => 'Economic analysis, statistics and policy for the public and private sectors.',
                        'overview' => 'Applied Economics combines microeconomic and macroeconomic theory with econometrics, development economics and data analysis, producing graduates who can evaluate policy and model real markets.',
                        'career_opportunities' => 'Economist, policy analyst, data analyst, research officer, banking and financial services, development sector.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 2400000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'Economics or Mathematics at principal level',
                                'required_subject' => 'Economics',
                                'minimum_grade' => 'D',
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE',
                                'minimum_points' => 8,
                            ],
                        ],
                    ],
                    [
                        'name' => 'Bachelor of Oil, Gas and Energy Management',
                        'code' => 'BOGEM',
                        'award' => 'Bachelor of Oil, Gas and Energy Management',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Business & Management',
                        'description' => 'Commercial, regulatory and operational management of Uganda\'s energy sector.',
                        'overview' => 'Built for Uganda\'s growing petroleum industry, this programme covers energy economics, contracts and licensing, local content, health and safety, environmental management and project finance.',
                        'career_opportunities' => 'Energy sector analyst, contracts and licensing officer, local content coordinator, HSE officer, energy project administrator.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 2800000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE',
                                'minimum_points' => 8,
                            ],
                        ],
                    ],
                    [
                        'name' => 'Diploma in Global Business',
                        'code' => 'DGB',
                        'award' => 'Diploma in Global Business',
                        'level' => Programme::LEVEL_DIPLOMA,
                        'department' => 'Department of Business & Management',
                        'description' => 'International trade, cross-border commerce and business communication.',
                        'overview' => 'A practical diploma covering international trade practice, logistics, digital commerce and intercultural business communication.',
                        'career_opportunities' => 'Trade officer, logistics coordinator, import/export administrator, business development assistant.',
                        'duration' => 2,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening', 'weekend'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 1800000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Certificate of Education with at least three credits',
                                'required_qualification' => 'uce',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Higher Education Certificate in Humanities',
                        'code' => 'HEC-HUM',
                        'award' => 'Higher Education Certificate',
                        'level' => Programme::LEVEL_CERTIFICATE,
                        'department' => 'Department of Law',
                        'description' => 'A one-year bridging certificate into law, business and social science degrees.',
                        'overview' => 'Prepares candidates for degree-level study in the humanities, with grounding in academic writing, reasoning and research method.',
                        'career_opportunities' => 'Progression to degree study in law, business, economics and the social sciences.',
                        'duration' => 1,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 1400000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Certificate of Education',
                                'required_qualification' => 'uce',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'name' => 'School of Science, Computing & Engineering',
                'code' => 'SSCE',
                'description' => 'Computing, geoscience and engineering for a digital and resource-rich economy.',
                'overview' => 'The School of Science, Computing & Engineering equips students to build and secure digital systems and to work responsibly with Uganda\'s natural resources. KCU took part in Uganda\'s inaugural National Cybersecurity Conference, reflecting the school\'s focus on security-led computing.',
                'departments' => ['Department of Computing & Information Security', 'Department of Earth & Petroleum Sciences'],
                'programmes' => [
                    [
                        'name' => 'Bachelor of Science in Computer Forensics',
                        'code' => 'BSCF',
                        'award' => 'Bachelor of Science in Computer Forensics',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Computing & Information Security',
                        'description' => 'Digital investigation, cybersecurity and evidence handling.',
                        'overview' => 'Students learn operating systems and networks from the ground up, then specialise in digital evidence acquisition, malware analysis, incident response and the legal frameworks governing cybercrime investigation.',
                        'career_opportunities' => 'Digital forensics analyst, cybersecurity analyst, incident responder, security consultant, law enforcement technical officer.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 2800000,
                        'is_featured' => true,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE',
                                'minimum_points' => 8,
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'UCE credit in Mathematics',
                                'required_subject' => 'Mathematics',
                                'minimum_grade' => 'C6',
                            ],
                        ],
                    ],
                    [
                        'name' => 'Bachelor of Science in Petroleum Geoscience',
                        'code' => 'BSPG',
                        'award' => 'Bachelor of Science in Petroleum Geoscience',
                        'level' => Programme::LEVEL_UNDERGRADUATE,
                        'department' => 'Department of Earth & Petroleum Sciences',
                        'description' => 'Geology, geophysics and reservoir science for petroleum exploration.',
                        'overview' => 'Covering structural geology, sedimentology, geophysical methods and reservoir characterisation, with field mapping in the Albertine Graben and laboratory analysis of core and log data.',
                        'career_opportunities' => 'Exploration geoscientist, geological data analyst, environmental geoscientist, mining and minerals sector, government regulation.',
                        'duration' => 3,
                        'duration_unit' => 'years',
                        'study_modes' => ['day'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 3000000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_SUBJECT,
                                'description' => 'Physics or Geography at principal level',
                                'required_subject' => 'Physics',
                                'minimum_grade' => 'D',
                            ],
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_UACE_POINTS,
                                'description' => 'At least two principal passes at UACE in science subjects',
                                'minimum_points' => 8,
                            ],
                        ],
                    ],
                    [
                        'name' => 'Higher Education Certificate in Physical Sciences',
                        'code' => 'HEC-PHY',
                        'award' => 'Higher Education Certificate',
                        'level' => Programme::LEVEL_CERTIFICATE,
                        'department' => 'Department of Computing & Information Security',
                        'description' => 'A one-year bridging certificate into computing, engineering and physical science degrees.',
                        'overview' => 'Strengthens mathematics, physics and computing fundamentals for candidates progressing to degree study in the physical sciences.',
                        'career_opportunities' => 'Progression to degree study in computing, engineering and the physical sciences.',
                        'duration' => 1,
                        'duration_unit' => 'years',
                        'study_modes' => ['day', 'evening'],
                        'campuses' => ['Bunga Campus'],
                        'tuition_amount' => 1400000,
                        'requirements' => [
                            [
                                'requirement_type' => ProgrammeRequirement::TYPE_QUALIFICATION,
                                'description' => 'Uganda Certificate of Education',
                                'required_qualification' => 'uce',
                            ],
                        ],
                    ],
                ],
            ],
        ];
    }
}
