<?php

function sendLiveClassNotification($conn_rjllearning, $conn_rjllearning_specific, $institution_id, $branch_id, $class_id, $section_id, $subject_id,$student_id, $message, $meeting_link, $start_time, $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';

    // Format the start time
    $formatted_time = date('d M Y, h:i A', strtotime($start_time));

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

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

    $format = "
    <p>You have a new live class scheduled for <strong>$subject_name</strong>.</p>
    <p><strong>Date & Time:</strong> $formatted_time</p>
    <p><strong>Meeting Link:</strong> <a href='$meeting_link'>$meeting_link</a></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';
    updateMailSmsCount($workspace_id, $institution_id, $branch_id, null, null, $student_count, 'notification', $conn_rjllearning);
    require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/firebase_notification/fcm_notifications_function.php';

    // Send the notification
    sendNotification($conn_rjllearning, $student_userids, "Live Class Notification", $format, 'live_class');

    // Update Mail and SMS count after sending notifications to all students
}
