<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\RoleModel;
use App\Models\PermissionModel;
use App\Models\RolePermissionModel;

class InitializeRolesPermissions extends Controller
{
    public function index()
    {
        $roleModel = new RoleModel();
        $permissionModel = new PermissionModel();
        $rolePermissionModel = new RolePermissionModel();

        $roles = [
            'administrator', 'principal', 'vice principal', 'admin', 'teacher', 'student', 'parent',
            'counselor', 'librarian', 'accountant', 'it support', 'receptionist', 'nurse', 'support staff', 
            'coach', 'activity coordinator'
        ];

        $permissions = [
            'manage_users',
            'view_reports',
            'manage_settings',
            'access_student_records',
            'communicate_parents',
            'enter_grades',
            'access_assignments',
            'view_own_records',
            'manage_library',
            'manage_finances',
            'provide_technical_support',
            'handle_inquiries',
            'provide_medical_assistance',
            'maintain_facilities',
            'manage_sports_teams',
            'organize_activities',
            'view_student_attendance',
            'manage_teacher_class_assignment',
            'conduct_training_sessions',
            'handle_disciplinary_actions',
            'update_curriculum',
            'conduct_examinations',
            'manage_transportation',
            'oversee_cafeteria_operations',
            'manage_alumni_relations',
            'conduct_research',
            'provide_guidance_counseling',
            'supervise_staff',
            'coordinate_events',
            'manage_social_media',
            'handle_emergency_situations',
            'monitor_student_progress',
            'maintain_security',
            'manage_inventory',
            'conduct_staff_meetings',
            'oversee_academic_advising',
            'manage_student_clubs',
            'coordinate_volunteer_programs',
            'provide_career_services',
            'oversee_internship_programs',
            'manage_community_outreach',
            'maintain_website',
            'administer_scholarships',
            'manage_student_housing',
            'oversee_student_council',
            'provide_mental_health_support',
            'manage_digital_resources',
            'coordinate_field_trips',
            'supervise_tutoring_services',
            'manage_parking_facilities',
            'administer_extracurricular_programs',
            'oversee_distance_learning',
            'manage_course_registration',
            'coordinate_staff_training',
            'provide_language_support',
            'supervise_after_school_programs',
            'handle_student_complaints',
            'manage_student_records',
            'oversee_financial_aid',
            'coordinate_health_services',
            'track_student_attendance',
            'manage_attendance_reports',
            'monitor_class_attendance',
            'update_attendance_records',
            'handle_attendance_inquiries',
            'generate_attendance_statistics',
            'communicate_attendance_issues',
            'oversee_attendance_policy',
            'enforce_attendance_requirements',
            'resolve_attendance_discrepancies',
            'manage_employee_attendance',
            'record_attendance_for_events',
            'track_daily_attendance',
            'maintain_attendance_logs',
            'analyze_attendance_data',
            'coordinate_attendance_training',
            'report_attendance_to_authorities'
        ];

        foreach ($roles as $role) {
            if (!$roleModel->where('name', $role)->first()) {
                $roleModel->save(['name' => $role]);
            }
        }

        foreach ($permissions as $permission) {
            if (!$permissionModel->where('name', $permission)->first()) {
                $permissionModel->save(['name' => $permission]);
            }
        }

        // Associating permissions with roles
        $rolePermissions = [
            'administrator' => ['manage_users', 'view_reports', 'manage_settings', 'access_student_records', 'communicate_parents', 'update_curriculum'],
            'principal' => ['manage_users', 'view_reports', 'access_student_records', 'communicate_parents', 'update_curriculum'],
            'vice principal' => ['view_reports', 'access_student_records', 'communicate_parents', 'update_curriculum'],
            'admin' => ['manage_users', 'view_reports', 'manage_settings', 'access_student_records', 'communicate_parents', 'update_curriculum'],
            'teacher' => ['access_student_records', 'communicate_parents', 'enter_grades', 'access_assignments'],
            'student' => ['view_own_records', 'access_assignments'],
            'parent' => ['view_own_records', 'communicate_parents'],
            'counselor' => ['access_student_records', 'communicate_parents'],
            'librarian' => ['manage_library'],
            'accountant' => ['manage_finances'],
            'it support' => ['provide_technical_support'],
            'receptionist' => ['handle_inquiries'],
            'nurse' => ['provide_medical_assistance'],
            'support staff' => ['maintain_facilities'],
            'coach' => ['manage_sports_teams'],
            'activity coordinator' => ['organize_activities']
        ];

        foreach ($rolePermissions as $role => $permissions) {
            $roleId = $roleModel->where('name', $role)->first()['id'];
            foreach ($permissions as $permission) {
                $permissionId = $permissionModel->where('name', $permission)->first()['id'];
                if (!$rolePermissionModel->where('role_id', $roleId)->where('permission_id', $permissionId)->first()) {
                    $rolePermissionModel->save(['role_id' => $roleId, 'permission_id' => $permissionId]);
                }
            }
        }

        echo "Roles and permissions have been initialized.";
    }
}

?>
