<?php

// function checkFacultyModuleGranted(
//     $function_code,
//     $class_section_subjects,
//     $class_incharge_roles,
//     $staff_access_modules,
//     $class_id,
//     $section_id,
//     $subject_id,
//     $conn_rjllearning
// ) {
//     // Fetch the group label associated with the function code
//     $group_label = fetchGroupLabel($function_code, $conn_rjllearning);
//     $task_type = determineTaskType($group_label);

//     // Normalize $section_id (handle both string and array inputs)
//     $section_id = normalizeSectionId($section_id);

//     // Only check for study-related tasks
//     if ($task_type == 'study_related') {
//         // Check Class ID
//         if ($class_id != "all") {
//             $unique_class_ids = array_unique(array_column(array_column($class_section_subjects, 'class'), 'class_id'));
//             if (!in_array($class_id, $unique_class_ids)) {
//                 errorreturn("Access denied: Invalid class ID.$class_id");
//             }
//         }

//         // Check Section ID
//         if ($section_id != "all") {
//             $section_found = false;

//             if ($class_id == 'all') {
//                 // Check if the section_id exists in any array element of class_section_subjects
//                 foreach ($class_section_subjects as $entry) {
//                     if ($entry['section']['section_id'] == $section_id) {
//                         $section_found = true;
//                         break;
//                     }
//                 }
//             } else {
//                 // Check if the section_id exists for the provided class_id
//                 foreach ($class_section_subjects as $entry) {
//                     if ($entry['class']['class_id'] == $class_id && $entry['section']['section_id'] == $section_id) {
//                         $section_found = true;
//                         break;
//                     }
//                 }
//             }

//             if (!$section_found) {
//                 errorreturn("Access denied: Section ID not found for this class");
//             }
//         }

//         // Check Subject ID (unchanged)
//         if ($subject_id != "all") {
//             $subject_found = false;

//             if ($class_id == 'all' && $section_id == 'all') {
//                 foreach ($class_section_subjects as $entry) {
//                     foreach ($entry['subjects'] as $subject_info) {
//                         if ($subject_info['subject_id'] == $subject_id) {
//                             $subject_found = true;
//                             break 2;
//                         }
//                     }
//                 }
//             } else if ($class_id != 'all' && $section_id == 'all') {
//                 foreach ($class_section_subjects as $entry) {
//                     if ($entry['class']['class_id'] == $class_id) {
//                         foreach ($entry['subjects'] as $subject_info) {
//                             if ($subject_info['subject_id'] == $subject_id) {
//                                 $subject_found = true;
//                                 break 2;
//                             }
//                         }
//                     }
//                 }
//             } else if ($class_id == 'all' && $section_id != 'all') {
//                 foreach ($class_section_subjects as $entry) {
//                     if ($entry['section']['section_id'] == $section_id) {
//                         foreach ($entry['subjects'] as $subject_info) {
//                             if ($subject_info['subject_id'] == $subject_id) {
//                                 $subject_found = true;
//                                 break 2;
//                             }
//                         }
//                     }
//                 }
//             } else if ($class_id != 'all' && $section_id != 'all') {
//                 foreach ($class_section_subjects as $entry) {
//                     if ($entry['class']['class_id'] == $class_id && $entry['section']['section_id'] == $section_id) {
//                         foreach ($entry['subjects'] as $subject_info) {
//                             if ($subject_info['subject_id'] == $subject_id) {
//                                 $subject_found = true;
//                                 break 2;
//                             }
//                         }
//                     }
//                 }
//             }

//             if (!$subject_found) {
//                 errorreturn("Access denied: Subject ID not found for this class and section.$subject_id");
//             }
//         }

//         return true;
//     } else {
//         // Non-study-related task: Check both class_incharge and staff access permissions
//         if ($class_id != null && $section_id != null) {
//             // Class Incharge Check
//             $class_incharge_granted = false;
//             if (!empty($class_incharge_roles)) {
//                 if ($class_id != "all") {
//                     $unique_class_ids = array_unique(array_column(array_column($class_incharge_roles, 'class'), 'class_id'));
//                     if (in_array($class_id, $unique_class_ids)) {
//                         $class_incharge_granted = true;
//                     }
//                 } else {
//                     $class_incharge_granted = true;
//                 }
                
//                 if ($class_incharge_granted && $section_id != "all") {
//                     $section_found = false;
//                     foreach ($class_incharge_roles as $entry) {
//                         if (
//                             ($class_id == 'all' || $entry['class']['class_id'] == $class_id) &&
//                             $entry['section']['section_id'] == $section_id
//                         ) {
//                             $section_found = true;
//                             break;
//                         }
//                     }
//                     $class_incharge_granted = $section_found;
//                 }
//             }

//             if ($class_incharge_granted) {
//                 return true;
//             }

//             // Staff Access Check
//             $staff_access_granted = false;

//             if (!empty($staff_access_modules) && !empty($staff_access_modules['class_section_modules'])) {
//                 if ($class_id != "all") {
//                     $unique_class_ids = array_unique(array_column(array_column($staff_access_modules['class_section_modules'], 'class'), 'class_id'));
//                     if (in_array($class_id, $unique_class_ids)) {
//                         $staff_access_granted = true;
//                     }
//                 } else {
//                     $staff_access_granted = true;
//                 }
//             }

//             if ($section_id != "all") {
//                 $section_found = false;

//                 if ($class_id == 'all') {
//                     foreach ($staff_access_modules['class_section_modules'] as $entry) {
//                         foreach ($entry['section'] as $section) {
//                             if ($section['section_id'] == $section_id) {
//                                 $section_found = true;
//                                 break 2;
//                             }
//                         }
//                     }
//                 } else {
//                     foreach ($staff_access_modules['class_section_modules'] as $entry) {
//                         if ($entry['class']['class_id'] == $class_id) {
//                             foreach ($entry['section'] as $section) {
//                                 if ($section['section_id'] == $section_id) {
//                                     $section_found = true;
//                                     break 2;
//                                 }
//                             }
//                         }
//                     }
//                 }

//                 if (!$section_found) {
//                     errorreturn("Access denied: Section ID not found for this class. Section ID: $section_id");
//                 }
//             }
//         }

//         $staff_access_granted = false;
//         if (!empty($staff_access_modules)) {
//             foreach ($staff_access_modules['modules_granted'] as $module) {
//                 if ($module['function_code'] === $function_code && $module['group_label'] === $group_label) {
//                     $staff_access_granted = true;
//                     break;
//                 }
//             }
//         }

//         if ($staff_access_granted) {
//             return true;
//         } else {
//             return false;
//         }
//     }
//     return false;
// }

function checkFacultyModuleGranted(
    $function_code,
    $class_section_subjects,
    $class_incharge_roles,
    $staff_access_modules,
    $class_id,
    $section_id,
    $subject_id,
    $conn_rjllearning
) {
    $group_label = fetchGroupLabel($function_code, $conn_rjllearning);
    $task_type = determineTaskType($group_label);

    $section_id = normalizeSectionId($section_id);

    $staff_access_granted = false;
    if (!empty($staff_access_modules)) {
        foreach ($staff_access_modules['modules_granted'] as $module) {
            if ($module['function_code'] === $function_code && $module['group_label'] === $group_label) {
                $staff_access_granted = true;
                break;
            }
        }
        
        if ($staff_access_granted) {
            $has_all_access = false;
            if (!empty($staff_access_modules['class_section_modules'])) {
                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    if ($module['class']['class_id'] === 'all') {
                        foreach ($module['section'] as $section) {
                            if ($section['section_id'] === 'all' || in_array('all', $section['section_id'])) {
                                $has_all_access = true;
                                break 2;
                            }
                        }
                    }
                }
            }
            
            if ($has_all_access) {
                return true;
            }
        }
    }

    // Only check for study-related tasks
    if ($task_type == 'study_related') {
        if ($class_id != "all") {
            $unique_class_ids = array_unique(array_column(array_column($class_section_subjects, 'class'), 'class_id'));
            
            $staff_class_access = false;
            if ($staff_access_granted && !empty($staff_access_modules['class_section_modules'])) {
                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    if ($module['class']['class_id'] == $class_id || $module['class']['class_id'] == 'all') {
                        $staff_class_access = true;
                        break;
                    }
                }
            }
            
            if (!in_array($class_id, $unique_class_ids) && !$staff_class_access) {
                // errorreturn("Access denied: Invalid class ID.$class_id");
                errorreturn("Access denied");
            }
        }

        // Check Section ID
        if ($section_id != "all") {
            $section_found = false;
            $staff_section_access = false;

            // Check staff access first
            if ($staff_access_granted && !empty($staff_access_modules['class_section_modules'])) {
                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    $module_class_id = $module['class']['class_id'];
                    $module_sections = $module['section'];
                    
                    // Check if this module grants access to the requested section
                    foreach ($module_sections as $section) {
                        $module_section_id = $section['section_id'];
                        
                        if ($module_section_id == 'all' || 
                            (is_array($module_section_id) && in_array('all', $module_section_id)) ||
                            $module_section_id == $section_id ||
                            (is_array($module_section_id) && in_array($section_id, $module_section_id))) {
                            
                            // Check if class also matches (if class is specified)
                            if ($class_id == 'all' || $module_class_id == 'all' || $module_class_id == $class_id) {
                                $staff_section_access = true;
                                break 2;
                            }
                        }
                    }
                }
            }

            if ($class_id == 'all') {
                // Check if the section_id exists in any array element of class_section_subjects
                foreach ($class_section_subjects as $entry) {
                    if ($entry['section']['section_id'] == $section_id) {
                        $section_found = true;
                        break;
                    }
                }
            } else {
                // Check if the section_id exists for the provided class_id
                foreach ($class_section_subjects as $entry) {
                    if ($entry['class']['class_id'] == $class_id && $entry['section']['section_id'] == $section_id) {
                        $section_found = true;
                        break;
                    }
                }
            }

            if (!$section_found && !$staff_section_access) {
                errorreturn("Access denied: Section ID not found for this class");
            }
        }

        // Check Subject ID (only for teaching assignments, not for staff access)
        if ($subject_id != "all" && !$staff_access_granted) {
            $subject_found = false;

            if ($class_id == 'all' && $section_id == 'all') {
                foreach ($class_section_subjects as $entry) {
                    foreach ($entry['subjects'] as $subject_info) {
                        if ($subject_info['subject_id'] == $subject_id) {
                            $subject_found = true;
                            break 2;
                        }
                    }
                }
            } else if ($class_id != 'all' && $section_id == 'all') {
                foreach ($class_section_subjects as $entry) {
                    if ($entry['class']['class_id'] == $class_id) {
                        foreach ($entry['subjects'] as $subject_info) {
                            if ($subject_info['subject_id'] == $subject_id) {
                                $subject_found = true;
                                break 2;
                            }
                        }
                    }
                }
            } else if ($class_id == 'all' && $section_id != 'all') {
                foreach ($class_section_subjects as $entry) {
                    if ($entry['section']['section_id'] == $section_id) {
                        foreach ($entry['subjects'] as $subject_info) {
                            if ($subject_info['subject_id'] == $subject_id) {
                                $subject_found = true;
                                break 2;
                            }
                        }
                    }
                }
            } else if ($class_id != 'all' && $section_id != 'all') {
                foreach ($class_section_subjects as $entry) {
                    if ($entry['class']['class_id'] == $class_id && $entry['section']['section_id'] == $section_id) {
                        foreach ($entry['subjects'] as $subject_info) {
                            if ($subject_info['subject_id'] == $subject_id) {
                                $subject_found = true;
                                break 2;
                            }
                        }
                    }
                }
            }

            if (!$subject_found) {
                errorreturn("Access denied: Subject ID not found for this class and section.$subject_id");
            }
        }
        return true;
    } else {
        // Non-study-related task: Check both class_incharge and staff access permissions
        if ($class_id != null && $section_id != null) {
            // Class Incharge Check
            $class_incharge_granted = false;
            if (!empty($class_incharge_roles)) {
                if ($class_id != "all") {
                    $unique_class_ids = array_unique(array_column(array_column($class_incharge_roles, 'class'), 'class_id'));
                    if (in_array($class_id, $unique_class_ids)) {
                        $class_incharge_granted = true;
                    }
                } else {
                    $class_incharge_granted = true;
                }
                
                if ($class_incharge_granted && $section_id != "all") {
                    $section_found = false;
                    foreach ($class_incharge_roles as $entry) {
                        if (
                            ($class_id == 'all' || $entry['class']['class_id'] == $class_id) &&
                            $entry['section']['section_id'] == $section_id
                        ) {
                            $section_found = true;
                            break;
                        }
                    }
                    $class_incharge_granted = $section_found;
                }
            }

            if ($class_incharge_granted) {
                return true;
            }

            // Staff Access Check
            $staff_access_granted_module = false;

            if (!empty($staff_access_modules) && !empty($staff_access_modules['class_section_modules'])) {
                if ($class_id != "all") {
                    $unique_class_ids = array_unique(array_column(array_column($staff_access_modules['class_section_modules'], 'class'), 'class_id'));
                    if (in_array($class_id, $unique_class_ids)) {
                        $staff_access_granted_module = true;
                    }
                } else {
                    $staff_access_granted_module = true;
                }
            }

            if ($section_id != "all") {
                $section_found = false;

                if ($class_id == 'all') {
                    foreach ($staff_access_modules['class_section_modules'] as $entry) {
                        foreach ($entry['section'] as $section) {
                            if ($section['section_id'] == $section_id) {
                                $section_found = true;
                                break 2;
                            }
                        }
                    }
                } else {
                    foreach ($staff_access_modules['class_section_modules'] as $entry) {
                        if ($entry['class']['class_id'] == $class_id) {
                            foreach ($entry['section'] as $section) {
                                if ($section['section_id'] == $section_id) {
                                    $section_found = true;
                                    break 2;
                                }
                            }
                        }
                    }
                }

                if (!$section_found) {
                    errorreturn("Access denied: Section ID not found for this class. Section ID: $section_id");
                }
            }
        }

        if ($staff_access_granted) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

function normalizeSectionId($section_id) {
    if (is_array($section_id)) {
        if (count($section_id) === 1) {
            return $section_id[0]; // Convert ["1"] → "1", ["all"] → "all"
        } elseif (in_array("all", $section_id)) {
            return "all"; // If array contains "all", treat as "all"
        } else {
            return $section_id[0]; // Fallback: take first element
        }
    }
    return $section_id; // Already a string
}

function facultyFilterQueryAppend(
    $sql,
    $array_params,
    $function_code,
    $class_section_subjects,
    $class_incharge_roles,
    $staff_access_modules,
    $class_id,
    $section_id,
    $subject_id,
    $conn_rjllearning,
    $class_multiple_bool = 1,
    $section_multiple_bool = 1,
    $subject_append = 1,
    $subject_multiple_bool = 1,
    $use_find_in_set_section = false,
    $use_find_in_set_in_class = false
) {


    $original_sql = $sql;
    $and_or_operator = 0; // if 0, append 'and' first time; otherwise append 'or'

    // Prepare section condition
    // $section_condition = $use_find_in_set
    //     ? "FIND_IN_SET(?, REPLACE(REPLACE(section_id, \"'\", \"\"), \"'\", \",\")) > 0"
    //     : "section_id = ?";
    $conditions_added = false;

    $group_label = fetchGroupLabel($function_code, $conn_rjllearning);

    // Determine the task type based on the group label
    $task_type = determineTaskType($group_label);

    $section_condition = $use_find_in_set_section
        ? "(section_id LIKE CONCAT('%', ?, '%') OR section_id = 'all')"
        : "section_id = ?";

    $class_condition = $use_find_in_set_in_class
        ? "(class_id LIKE CONCAT('%', ?, '%') OR class_id = 'all')"
        : "class_id = ?";

    if ($task_type == 'study_related') {
        // Case 1: All, Specific, Specific
        if ($class_id == 'all' && $section_id != 'all' && $subject_id != 'all') {
            foreach ($class_section_subjects as $element) {
                if ($element['section']['section_id'] == $section_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        if ($temp_subject['subject_id'] == $subject_id) {
                            $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                            $sql .= "($class_condition and $section_condition and subject_id = ?)";
                            array_push($array_params, $element['class']['class_id'], $section_id, $subject_id);
                            $and_or_operator++;
                        }
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 2: Specific, Specific, Specific
        if ($class_id != 'all' && $section_id != 'all' && $subject_id != 'all') {

            foreach ($class_section_subjects as $element) {
                if ($element['class']['class_id'] == $class_id && $element['section']['section_id'] == $section_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        if ($temp_subject['subject_id'] == $subject_id) {
                            $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                            $sql .= "($class_condition and $section_condition and subject_id = ?)";
                            array_push($array_params, $class_id, $section_id, $subject_id);
                            $and_or_operator++;
                        }
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 3: Specific, Specific, All
        if ($class_id != 'all' && $section_id != 'all' && $subject_id == 'all') {
            foreach ($class_section_subjects as $element) {
                if ($element['class']['class_id'] == $class_id && $element['section']['section_id'] == $section_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition and subject_id = ?)";
                        array_push($array_params, $class_id, $section_id, $temp_subject['subject_id']);
                        $and_or_operator++;
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 4: Specific, All, Specific
        if ($class_id != 'all' && $section_id == 'all' && $subject_id != 'all') {
            foreach ($class_section_subjects as $element) {
                if ($element['class']['class_id'] == $class_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        if ($temp_subject['subject_id'] == $subject_id) {
                            $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                            $sql .= "($class_condition and $section_condition and subject_id = ?)";
                            array_push($array_params, $class_id, $element['section']['section_id'], $subject_id);
                            $and_or_operator++;
                        }
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 5: Specific, All, All
        if ($class_id != 'all' && $section_id == 'all' && $subject_id == 'all') {
            foreach ($class_section_subjects as $element) {
                if ($element['class']['class_id'] == $class_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition and subject_id = ?)";
                        array_push($array_params, $class_id, $element['section']['section_id'], $temp_subject['subject_id']);
                        $and_or_operator++;
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 6: All, Specific, All
        if ($class_id == 'all' && $section_id != 'all' && $subject_id == 'all') {
            foreach ($class_section_subjects as $element) {
                if ($element['section']['section_id'] == $section_id) {
                    foreach ($element['subjects'] as $temp_subject) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition and subject_id = ?)";
                        array_push($array_params, $element['class']['class_id'], $section_id, $temp_subject['subject_id']);
                        $and_or_operator++;
                    }
                }
            }
            if ($and_or_operator > 0)
                $sql .= ')';
        }

        // Case 7: All, All, Specific
        if ($class_id == 'all' && $section_id == 'all' && $subject_id != 'all') {
            foreach ($class_section_subjects as $element) {
                foreach ($element['subjects'] as $temp_subject) {
                    if ($temp_subject['subject_id'] == $subject_id) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition and subject_id = ?)";
                        array_push($array_params, $element['class']['class_id'], $element['section']['section_id'], $subject_id);
                        $and_or_operator++;
                    }
                }
            }
            if ($and_or_operator > 0) {
                $sql .= ')';
            }
        }

        // Case 8: All, All, All
        if ($class_id == 'all' && $section_id == 'all' && $subject_id == 'all') {
            foreach ($class_section_subjects as $element) {
                foreach ($element['subjects'] as $temp_subject) {
                    $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                    $sql .= "($class_condition and $section_condition and subject_id = ?)";
                    array_push($array_params, $element['class']['class_id'], $element['section']['section_id'], $temp_subject['subject_id']);
                    $and_or_operator++;
                }
            }
            if ($and_or_operator > 0) {
                $sql .= ')';
            }
        }
    }
    if ($task_type == 'class_incharge') {

        // Handle class incharge roles filtering
        if ($class_incharge_roles) {
            // Case 1: `class_id` = 'all', specific `section_id`
            if ($class_id == 'all' && $section_id != 'all') {
                foreach ($class_incharge_roles as $element) {
                    if ($element['section']['section_id'] == $section_id) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition)";
                        array_push($array_params, $element['class']['class_id'], $section_id);
                        $and_or_operator++;
                        $conditions_added = true; // Set flag to true
                    }
                }
                if ($and_or_operator > 0)
                    $sql .= ')';
            }

            // Case 2: `class_id` = 'all', `section_id` = 'all'
            if ($class_id == 'all' && $section_id == 'all') {

                foreach ($class_incharge_roles as $element) {
                    $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                    $sql .= "($class_condition and $section_condition)";
                    array_push($array_params, $element['class']['class_id'], $element['section']['section_id']);
                    $and_or_operator++;
                    $conditions_added = true; // Set flag to true
                }
                if ($and_or_operator > 0)
                    $sql .= ')';
            }

            // Case 3: specific `class_id`, specific `section_id`
            if ($class_id != 'all' && $section_id != 'all') {


                foreach ($class_incharge_roles as $element) {

                    if ($element['class']['class_id'] == $class_id && $element['section']['section_id'] == $section_id) {

                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition)";

                        array_push($array_params, $class_id, $section_id);
                        $and_or_operator++;
                        $conditions_added = true; // Set flag to true
                    }
                }
                if ($and_or_operator > 0)
                    $sql .= ')';
            }

            // Case 4: specific `class_id`, `section_id` = 'all'
            if ($class_id != 'all' && $section_id == 'all') {
                foreach ($class_incharge_roles as $element) {
                    if ($element['class']['class_id'] == $class_id) {
                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition)";
                        array_push($array_params, $class_id, $element['section']['section_id']);
                        $and_or_operator++;
                        $conditions_added = true; // Set flag to true
                    }
                }
                if ($and_or_operator > 0)
                    $sql .= ')';
            }
        }
    }
    if ($task_type == 'staff_access') {


        if ($staff_access_modules && isset($staff_access_modules['class_section_modules']) && !$conditions_added) {
            $and_or_operator = 0; // Reset for staff access modules

            // Case 1: `class_id` = 'all' and `section_id` = 'all`
            if ($class_id == 'all' && $section_id == 'all') {
                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    $module_class_id = $module['class']['class_id'];

                    foreach ($module['section'] as $section) {
                        $module_section_id = $section['section_id'];

                        $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                        $sql .= "($class_condition and $section_condition)";
                        array_push($array_params, $module_class_id, $module_section_id);
                        $and_or_operator++;
                    }
                }
            }

            // Case 2: `class_id` = 'all' and specific `section_id`
            if ($class_id == 'all' && $section_id != 'all') {

                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    $module_class_id = $module['class']['class_id'];

                    foreach ($module['section'] as $section) {
                        $module_section_id = $section['section_id'];

                        if ($module_section_id == $section_id || $module_section_id == 'all') {
                            $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                            $sql .= "($class_condition and $section_condition)";
                            array_push($array_params, $module_class_id, $section_id);
                            $and_or_operator++;
                        }
                    }
                }
            }

            // Case 3: specific `class_id` and `section_id` = 'all'
            if ($class_id != 'all' && $section_id == 'all') {
                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    $module_class_id = $module['class']['class_id'];

                    if ($module_class_id == $class_id) {
                        foreach ($module['section'] as $section) {
                            $module_section_id = $section['section_id'];

                            $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                            $sql .= "($class_condition and $section_condition)";
                            array_push($array_params, $class_id, $module_section_id);
                            $and_or_operator++;
                        }
                    }
                }
            }

            // Case 4: specific `class_id` and specific `section_id`
            if ($class_id != 'all' && $section_id != 'all') {

                foreach ($staff_access_modules['class_section_modules'] as $module) {
                    $module_class_id = $module['class']['class_id'];

                    if ($module_class_id == $class_id) {
                        foreach ($module['section'] as $section) {
                            $module_section_id = $section['section_id'];

                            if ($module_section_id == $section_id || $module_section_id == 'all') {
                                $sql .= ($and_or_operator == 0) ? ' and (' : ' or ';
                                $sql .= "($class_condition and $section_condition)";
                                array_push($array_params, $class_id, $section_id);
                                $and_or_operator++;
                            }
                        }
                    }
                }
            }

            // Close the parenthesis if any conditions were added
            if ($and_or_operator > 0) {
                $sql .= ')';
            }
        }

    }

    return array('sql' => $sql, 'array_params' => $array_params);
}

// Function to determine task type based on function code
function determineTaskType($function_code)
{
    $functionCategories = [
        'study_related' => ['Lesson', 'Homework', 'Test', 'Lesson Category', 'Subjectwise Attendance', 'Assignments', 'Assignment Submission', 'Test', 'Class Meeting'],
        'class_incharge' => ['ApplyLeaves', 'Attendance', 'Announcement','Students & Registration','Students Admission','Students', 'Homework', 'Gate Pass'],
        'staff_access' => [
            'Library Management',
            'Accounting',
            'ApplyLeaves',
            'Manage Alumni',
            'Marksheets',
            'Students Admission',
            'Student Id Cards',
            'Students & Registration',
            'Visitors',
            'Gate Pass',
            'Students',
            'Activity Mentor',
            'Activity Corner',
            'Receipts And Expenses',
            'Fuel expense',
            'Transport',
            'Route',
            'Id_Cards',
            'Staff_Access_Support',
            'Reminders',
            'Class Meeting',
            'Timetable',
            'Lesson', 'Homework', 'Test', 'Lesson Category', 'Subjectwise Attendance', 'Assignments', 'Assignment Submission', 'Test',
            'ApplyLeaves', 'Attendance', 'Announcement'
        ]
    ];

    foreach ($functionCategories as $task_type => $functions) {
        if (in_array($function_code, $functions)) {

            return $task_type;
        }
    }

    return null;
}


function fetchGroupLabel($function_code, $conn_rjllearning)
{

    $query = "SELECT group_label FROM function_codes WHERE code = ?";
    $stmt = $conn_rjllearning->prepare($query);
    $stmt->execute([$function_code]);

    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    return $result ? $result['group_label'] : null;
}


function getTaskType($function_code, $conn_rjllearning)
{
    $group_label = fetchGroupLabel($function_code, $conn_rjllearning);
    $task_type = determineTaskType($group_label);

    return [$group_label, $task_type];
}
