<?php

function getStudentInfo($conn_rjllearning_specific, $institution_id, $student_id)
{

    $res = $conn_rjllearning_specific->prepare("SELECT id,name FROM students WHERE id = ? and institution_id=?");
    $res->execute(array($student_id, $institution_id));
    $result_faculty = $res->fetchAll();
    if (count($result_faculty) > 0) {
        $student_info =  array("student_id" => $result_faculty[0]['id'], "name" => $result_faculty[0]['name']);
    } else {
        errorreturn("Faculty not found.");
    }
    return $student_info;
}




function getStudentsForNotification($conn_rjllearning_specific, $institution_id, $branch_id, $class_id = null, $section_id = null, $student_id = null, $additionaldata = null)
{

    try {
        // Initialize the base query
        $sql = "SELECT ";

        // Add the columns dynamically
        if ($additionaldata && is_array($additionaldata)) {
            $sql .= implode(", ", $additionaldata);
        } else {
            $sql .= "*"; // Default to selecting all columns if no additional data is provided
        }

        $sql .= " FROM students WHERE institution_id = ?  AND can_activate = 1";

        // Initialize the array of parameters
        $array_params = [$institution_id];
        if ($branch_id !== null) {
            if ($branch_id === 'all') {
                // If 'all', match all branch_ids (no condition needed)
            } else {
                $branch_id = json_decode($branch_id, true);
                if (is_array($branch_id)) {
                    // Format branch_id values for the IN clause
                    $imploded_branch_ids = implode(",", array_map('intval', $branch_id)); // Convert each to integer for security
                    $sql .= " AND branch_id IN ($imploded_branch_ids)";
                } else {
                    // If branch_id is a single value, handle it as such
                    $sql .= " AND branch_id = ?";
                    $array_params[] = $branch_id;
                }
            }
        }

        // Add class_id to the query if provided
        if ($class_id !== null) {
            // Check if the value is 'all', in which case we include all class_ids
            if ($class_id === 'all') {
                // If 'all', don't add the class_id condition to the query
            } else {
                $sql .= " AND class_id = ?";
                $array_params[] = $class_id;
            }
        }


        // Add section_id to the query if provided
        if ($section_id !== null) {

            if ($section_id === 'all') {
                // If 'all', don't add the section_id condition to the query
            } else {
                $section_id = json_decode($section_id, true);
                if (is_array($section_id)) {

                    // Format section_id values for the IN clause
                    $imploded_section_ids = implode(",", array_map('intval', $section_id)); // Convert each to integer for security
                    $sql .= " AND section_id IN ($imploded_section_ids)";
                } else {

                    // If section_id is a single value, handle it as such
                    $sql .= " AND section_id = ?";
                    $array_params[] = $section_id;
                }
            }
        }
        // Add student_id to the query if provided
        if (!empty($student_id)) {

            if ($student_id === 'all') {
                // If 'all', don't add the student_id condition to the query
            } else {
                // Ensure $student_id is treated as an array
                if (!is_array($student_id)) {
                    $student_id = [$student_id]; // Convert to an array if it's a single value
                }

                // Format student IDs for the IN clause
                $imploded_student_ids = implode(",", array_map(function ($id) {
                    return "'" . (int)$id . "'"; // Safely ensure each student_id is treated as an integer and quoted
                }, $student_id));

                $sql .= " AND id IN ($imploded_student_ids)";
            }
        }
        // Order the results by name
        $sql .= " ORDER BY name ASC";

        // Prepare and execute the SQL query
        $stmt = $conn_rjllearning_specific->prepare($sql);
        $stmt->execute($array_params);

        // Fetch the results
        $students = $stmt->fetchAll(PDO::FETCH_ASSOC);

        // Return the list of students, or an empty array if no students were found
        return $students;
    } catch (PDOException $e) {
        // Handle database errors (but allow process to continue)
        return []; // Return an empty array instead of error if database issues occur
    }
}
