<?php
function sendLessonNotification($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $subject_id, $message, $workspace_id, $web_app_title) {
    // Additional student data to fetch
    
    require_once $GLOBALS['base_path_to_learning'] . 'admin_api/modules/students/student_functions.php';
    $additionaldata = ['id', 'institution_id', 'branch_id', 'admission_number', 'name', 'mobile', 'email', 'fathername', 'user_id'];

    // Step 1: Get all students in the specified class and section
    $students = getStudentsForNotification($conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $student_id = null, $additionaldata);
    if (empty($students)) {
        return;  // No students found, stop execution
    }
    // Get subject information
    $subject_info = getSubject($conn_rjllearning_specific, $institution_id, $subject_id);
    $subject_name = isset($subject_info['subject_name']) ? $subject_info['subject_name'] : 'Unknown Subject';

    // Step 2: Loop through each student and send the lesson notification
    $student_count = count($students);

    // Prepare the email subject and content
    $subject = 'Lesson Notification - ' . $web_app_title;

    $format = "
    <p>You have a new lesson for <strong>$subject_name</strong>.</p>
    <p>$message</p>
    ";

    // Collect student user IDs for notification
    $student_userids = array_map(function($student) {
        return $student['user_id'];
    }, $students);

    // Step 3: Fetch all FCM tokens for the collected user_ids
    require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/function/function.php'; 
    require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/firebase_notification/fcm_notifications_function.php';
   

    // $tokens = getUserFCMToken($conn_rjllearning, $student_userids);

    // Send the notification
    sendNotification($conn_rjllearning,$student_userids, "Lesson Notification", $format, 'lesson');

    // Update Mail and SMS count after sending notifications to all students
    updateMailSmsCount($workspace_id, $institution_id, $branch_id, null, null, $student_count, 'notification', $conn_rjllearning);
}
