<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class NavSeeder extends Seeder
{
    public function run()
    {
        $db = \Config\Database::connect();

        // Top-level groups (dropdown headers are just parents with no URL)
        $groups = [
            ['label'=>'Users',            'url'=>null, 'sort_order'=>10],
            ['label'=>'Roles',            'url'=>null, 'sort_order'=>20],
            ['label'=>'Configuration',    'url'=>null, 'sort_order'=>30],
            ['label'=>'Staffing',         'url'=>null, 'sort_order'=>40],
            ['label'=>'Parents',          'url'=>null, 'sort_order'=>50],
            ['label'=>'Student-Affairs',  'url'=>null, 'sort_order'=>60],
            ['label'=>'Classes',          'url'=>null, 'sort_order'=>70],
            ['label'=>'Communication',    'url'=>null, 'sort_order'=>80],
            ['label'=>'Financial',        'url'=>null, 'sort_order'=>90],
            ['label'=>'Printables',       'url'=>null, 'sort_order'=>100],
            ['label'=>'Event Management', 'url'=>null, 'sort_order'=>110],
        ];

        $builder = $db->table('nav_items');
        $ids = [];
        foreach ($groups as $g) {
            $builder->insert([
                'parent_id'  => null,
                'label'      => $g['label'],
                'url'        => null,
                'sort_order' => $g['sort_order'],
                'is_enabled' => 1,
                'created_at' => utc_now()
            ]);
            $ids[$g['label']] = $db->insertID();
        }

        // Children for Users
        $items = [
            ['parent'=>'Users', 'label'=>'Active Notifications', 'url'=>'notifications/active', 'sort_order'=>1],
            ['parent'=>'Users', 'label'=>'Deleted Notifications','url'=>'notifications/deleted','sort_order'=>2],
            ['parent'=>'Users', 'label'=>'Login Activity',       'url'=>'user/login_activity', 'sort_order'=>3],
            ['parent'=>'Users', 'label'=>'User List',            'url'=>'user/user_list',      'sort_order'=>4],

            // Roles
            ['parent'=>'Roles', 'label'=>'Assign User Role',     'url'=>'rolepermission/assign_role', 'sort_order'=>1],
            ['parent'=>'Roles', 'label'=>'Role Management',      'url'=>'rolepermission/roles',       'sort_order'=>2],

            // Configuration
            ['parent'=>'Configuration','label'=>'Add/Edit Configuration','url'=>'configuration/configuration_view','sort_order'=>1],
            ['parent'=>'Configuration','label'=>'School Years','url'=>'administrator/school-years','sort_order'=>2],

            // Staffing
            ['parent'=>'Staffing','label'=>'Staff Profile','url'=>'staff/index','sort_order'=>1],
            ['parent'=>'Staffing','label'=>'Teacher Class Assignment','url'=>'administrator/teacher_class_assignment','sort_order'=>2],

            // Parents
            ['parent'=>'Parents','label'=>'Parent Profile','url'=>'administrator/parent_profiles','sort_order'=>1],

            // Student-Affairs
            ['parent'=>'Student-Affairs','label'=>'Attendance Management','url'=>'administrator/daily_attendance','sort_order'=>1],
            ['parent'=>'Student-Affairs','label'=>'Attendance Tracking System','url'=>'attendance/violations','sort_order'=>2],
            ['parent'=>'Student-Affairs','label'=>'Attendance Scans','url'=>'rfid_coming_soon','sort_order'=>3],
            ['parent'=>'Student-Affairs','label'=>'Classes List','url'=>'administrator/class_assignment','sort_order'=>4],
            ['parent'=>'Student-Affairs','label'=>'Emergency Contact','url'=>'administrator/emergency_contact','sort_order'=>5],
            ['parent'=>'Student-Affairs','label'=>'Enrollment-Withdrawal','url'=>'enroll_withdraw/enrollment_withdrawal','sort_order'=>6],
            ['parent'=>'Student-Affairs','label'=>'Flags Management','url'=>'flags/flags_management','sort_order'=>7],
            ['parent'=>'Student-Affairs','label'=>'School Calendar','url'=>'administrator/calendar_view','sort_order'=>8],
            ['parent'=>'Student-Affairs','label'=>'Score Analysis','url'=>'report/combined','sort_order'=>9],
            ['parent'=>'Student-Affairs','label'=>'Score Management','url'=>'grading','sort_order'=>10],
            ['parent'=>'Student-Affairs','label'=>'Student Class Assignment','url'=>'administrator/student_class_assignment','sort_order'=>11],
            ['parent'=>'Student-Affairs','label'=>'Student Profile','url'=>'administrator/student_profiles','sort_order'=>12],

            // Classes
            ['parent'=>'Classes','label'=>'Classes List','url'=>'administrator/class_assignment','sort_order'=>1],

            // Communication
            ['parent'=>'Communication','label'=>'Parent Email Extractor','url'=>'emails/parent_email_extractor','sort_order'=>1],
            ['parent'=>'Communication','label'=>'Parent Profile','url'=>'administrator/parent_profiles','sort_order'=>2],
            ['parent'=>'Communication','label'=>'Student Profile','url'=>'administrator/student_profiles','sort_order'=>3],

            // Financial
            ['parent'=>'Financial','label'=>'Discount Management','url'=>'discounts/list','sort_order'=>1],
            ['parent'=>'Financial','label'=>'Expenses Management','url'=>'expenses/index','sort_order'=>2],
            ['parent'=>'Financial','label'=>'Financial Report','url'=>'payment/financial_report','sort_order'=>3],
            ['parent'=>'Financial','label'=>'Tuition Forecast','url'=>'administrator/tuition-forecast','sort_order'=>4],
            ['parent'=>'Financial','label'=>'Invoices Management','url'=>'invoice_payment/invoice_management','sort_order'=>5],
            ['parent'=>'Financial','label'=>'Manual Payment','url'=>'payment/manual_pay','sort_order'=>6],
            ['parent'=>'Financial','label'=>'Refund Management','url'=>'refunds/list','sort_order'=>7],
            ['parent'=>'Financial','label'=>'Reimbursement Management','url'=>'reimbursements/index','sort_order'=>8],

            // Printables
            ['parent'=>'Printables','label'=>'Badges','url'=>'printables_reports/badge_form','sort_order'=>1],
            ['parent'=>'Printables','label'=>'Class Prep','url'=>'class-prep','sort_order'=>2],
            ['parent'=>'Printables','label'=>'Report Cards','url'=>'printables_reports/report_card','sort_order'=>3],
            ['parent'=>'Printables','label'=>'Stickers','url'=>'printables_reports/stickers','sort_order'=>4],

            // Event Management
            ['parent'=>'Event Management','label'=>'Calendar','url'=>'administrator/calendar','sort_order'=>1],
            ['parent'=>'Event Management','label'=>'Events','url'=>'administrator/events','sort_order'=>2],
        ];

        foreach ($items as $it) {
            $builder->insert([
                'parent_id'  => $ids[$it['parent']] ?? null,
                'label'      => $it['label'],
                'url'        => $it['url'],
                'sort_order' => $it['sort_order'],
                'is_enabled' => 1,
                'created_at' => utc_now(),
            ]);
        }

        // Map roles quickly: give full menu to administrator & principal; a subset to others
        $roleMap = $db->table('role_nav_items');
        $navRows = $db->table('nav_items')->orderBy('id')->get()->getResultArray();

        $grantAllTo = ['administrator','principal','vice_principal'];
        foreach ($navRows as $row) {
            foreach ($grantAllTo as $role) {
                $roleMap->insert([
                    'role'        => strtolower($role),
                    'nav_item_id' => $row['id'],
                    'created_at'  => utc_now()
                ]);
            }
        }

        // Example: "admin" (limited)
        $limitForAdmin = ['Enrollment-Withdrawal','Invoices Management'];
        foreach ($navRows as $row) {
            if (in_array($row['label'], $limitForAdmin, true)) {
                $roleMap->insert([
                    'role'        => 'admin',
                    'nav_item_id' => $row['id'],
                    'created_at'  => utc_now()
                ]);
            }
        }

        // Example: HOD Communication
        $hodComm = ['Communication','Parent Profile','Student Profile','Parent Email Extractor'];
        foreach ($navRows as $row) {
            if (in_array($row['label'], $hodComm, true)) {
                $roleMap->insert([
                    'role'        => 'head of department (communication)',
                    'nav_item_id' => $row['id'],
                    'created_at'  => utc_now()
                ]);
            }
        }

        // Example: HOD Education (Student-Affairs group)
        $hodEduLabels = ['Student-Affairs','Attendance Management','Classes List','Emergency Contact','Enrollment-Withdrawal','Flags Management','School Calendar','Score Analysis','Score Management','Student Class Assignment','Student Profile'];
        foreach ($navRows as $row) {
            if (in_array($row['label'], $hodEduLabels, true)) {
                $roleMap->insert([
                    'role'        => 'head of department (education)',
                    'nav_item_id' => $row['id'],
                    'created_at'  => utc_now()
                ]);
            }
        }

        // Example: HOD IT (Grades)
        foreach ($navRows as $row) {
            if (in_array($row['label'], ['Score Management'], true)) {
                $roleMap->insert([
                    'role'        => 'head of department (information technology)',
                    'nav_item_id' => $row['id'],
                    'created_at'  => utc_now()
                ]);
            }
        }
    }
}
