// <?php
// function fetch_specific_manage_alumni($conn_rjllearning_specific, $left_students_id, $institution_id, $branch_id)
// {
//     // var_dump($left_students_id);
//     // die();

//     $sql_fetch_left_students = "SELECT * FROM left_students WHERE id = ? AND institution_id = ?";
//     $array_params_fetch_left_students = [$left_students_id, $institution_id];

//     try {
//         $sql_fetch_left_students_stmt = $conn_rjllearning_specific->prepare($sql_fetch_left_students);
//         $sql_fetch_left_students_stmt->execute($array_params_fetch_left_students);
//         $left_students = $sql_fetch_left_students_stmt->fetchAll(PDO::FETCH_ASSOC);

//         if (!empty($left_students)) {
//             $response = [];
//             $student_sql = "SELECT * FROM students WHERE institution_id = ? AND id = ?";
//             foreach ($left_students as $alumni) {
//                 $student_id = $alumni['student_id'];

//                 $student_stmt = $conn_rjllearning_specific->prepare($student_sql);
//                 $student_stmt->execute([$institution_id, $student_id]);
//                 $student_result = $student_stmt->fetchAll(PDO::FETCH_ASSOC);

//                 $student_id = $student_result[0]['id'];
//                 $student_name = $student_result[0]['name'];
//                 $student_father_name = $student_result[0]['fathername'];
//                 $student_email = $student_result[0]['email'];
//                 $student_gender = $student_result[0]['gender'];
//                 $student_mothername = $student_result[0]['mothername'];
//                 $student_mobile = $student_result[0]['mobile'];
//                 $student_address_line_1 = $student_result[0]['address_line_1'];
//                 $student_city = $student_result[0]['city'];
//                 $student_student_profile_image = $student_result[0]['student_profile_image'];

//                 if ($student_student_profile_image != null && !is_array($student_student_profile_image)) {
//                     $data_attachment = json_decode($student_student_profile_image, true);
//                     $filePath = $data_attachment['filepath'];
//                     $originalName = $data_attachment['original_name'];
//                     $aws_attachment = displayFileFromS3($GLOBALS['s3_learning_bucket'], $GLOBALS['learning_bucketName'], $filePath);
//                     $final_student_profile_image = ["filepath" => $aws_attachment, "original_name" => $originalName];
//                 } else {
//                     $final_student_profile_image = null;
//                 }

//                 // if ($alumni['date_application_certificate'] != null && !is_array($alumni['date_application_certificate'])) {
//                 //     $data_attachment = json_decode($alumni['date_application_certificate'], true);
//                 //     $filePath = $data_attachment['filepath'];
//                 //     $originalName = $data_attachment['original_name'];
//                 //     $aws_attachment = displayFileFromS3($GLOBALS['s3_learning_bucket'], $GLOBALS['learning_bucketName'], $filePath);
//                 //     $final_array_date_application_certificate = ["filepath" => $aws_attachment, "original_name" => $originalName];
//                 // } else {
//                 //     $final_array_date_application_certificate = null;
//                 // }

//                 // if ($alumni['date_issue_certificate'] != null && !is_array($alumni['date_issue_certificate'])) {
//                 //     $data_attachment = json_decode($alumni['date_issue_certificate'], true);
//                 //     $filePath = $data_attachment['filepath'];
//                 //     $originalName = $data_attachment['original_name'];
//                 //     $aws_attachment = displayFileFromS3($GLOBALS['s3_learning_bucket'], $GLOBALS['learning_bucketName'], $filePath);
//                 //     $final_array_date_issue_certificate = ["filepath" => $aws_attachment, "original_name" => $originalName];
//                 // } else {
//                 //     $final_array_date_issue_certificate = null;
//                 // }

//                 $class_info = getClass($conn_rjllearning_specific, $institution_id, $alumni['class_id']);
//                 $branch_info = getBranchInfo($conn_rjllearning_specific, $institution_id, $alumni['branch_id']);

//                 $section_info = [];
//                 $section_id = $alumni['section_id'];
//                 if ($section_id == 'all') {
//                     $section_info[] = ['section_id' => $section_id, 'section_name' => ucwords($section_id)];
//                 } else {
//                     $sql = "SELECT * FROM section_info WHERE institution_id = ? AND id = ?";
//                     $stmt = $conn_rjllearning_specific->prepare($sql);
//                     $stmt->execute([$institution_id, $section_id]);
//                     $section_data = $stmt->fetch(PDO::FETCH_ASSOC);
//                     if ($section_data) {
//                         $section_info[] = ['section_id' => $section_data['id'], 'section_name' => $section_data['section_name']];
//                     }
//                 }

//               $subject_studied = [];

// $raw = $alumni['subject_studied'] ?? '';

// $first_decode = json_decode($raw, true);

// if (json_last_error() === JSON_ERROR_NONE) {
//     // Case 1: Already an array of subject objects (Format 2)
//     if (is_array($first_decode) && isset($first_decode[0]) && is_array($first_decode[0])) {
//         $subject_studied = $first_decode;

//     // Case 2: A JSON-encoded string that needs another decode (Format 1)
//     } elseif (is_string($first_decode)) {
//         $second_decode = json_decode($first_decode, true);
//         if (json_last_error() === JSON_ERROR_NONE && is_array($second_decode)) {
//             $subject_studied = $second_decode;
//         }
//     }
// }


//                 $personal_info = [
//                     'student_id' => $student_id,
//                     'name' => $student_name,
//                     'fathername' => $student_father_name,
//                     'mothername' => $student_mothername,
//                     'mobile' => $student_mobile,
//                     'gender' => $student_gender,
//                     'email' => $student_email,
//                     'student_profile_image' => $final_student_profile_image,
//                     'city' => $student_city,
//                     'address_line_1' => $student_address_line_1,
//                     'admission_number' => $alumni['admission_number'],
//                     'nationality' => $alumni['nationality'],
//                     'caste' => $alumni['caste'],
//                     'date_admission' => $alumni['date_admission'],
//                     'dob' => $alumni['dob'],
//                     'status' => $alumni['status'] ?? null,
//                 ];

//                 $other_info = [
//                     'school_last_examination' => $alumni['school_last_examination'],
//                     'failed_same_class' => $alumni['failed_same_class'],
//                     'subject_studied' => $subject_studied,
//                     'last_class' => getClass($conn_rjllearning_specific, $institution_id, $alumni['class_id']),
//                     'promotion_higher_class' => $alumni['promotion_higher_class'],
//                     'paid' => $alumni['paid'],
//                     'fee_concession' => $alumni['fee_concession'],
//                     'working_days' => $alumni['working_days'],
//                     'board' => $alumni['board'],
//                     'ncc' => $alumni['ncc'],
//                     'conduct' => $alumni['conduct'],
//                     'present_days' => $alumni['present_days'],
//                     'class_in_figure' => $alumni['class_in_figure'],
//                     'extra_curricular_activities' => $alumni['extra_curricular_activities'],
//                     'date_application_certificate' => $alumni['date_application_certificate'],
//                     'date_issue_certificate' => $alumni['date_issue_certificate'],
//                     'reasons_leaving_school' => $alumni['reasons_leaving_school'],
//                     'remarks' => $alumni['remarks'],
//                 ];

//                 $response = [
//                     'left_students_id' => $alumni['id'],
//                     'class_info' => $class_info,
//                     'branch_info' => $branch_info,
//                     'section_info' => $section_info,
//                     'personal_info' => $personal_info,
//                     'other_info' => $other_info,
//                 ];
//             }
//             return $response;
//         } else {
//             errorreturn("No alumni found for the given parameters.");
//         }
//     } catch (PDOException $e) {
//         errorreturn("Database error: " . $e->getMessage());
//     }
// }

