<?php
ini_set('display_errors', 1);
date_default_timezone_set('Asia/Kolkata');
require_once '/home/rejoicebilldesk/public_html/rjllearning_development/resources/base_path_include.php';

require_once $base_path_to_learning_resources  . "includes/connect.php"; // use require for mandatory files
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';

//valdation file
require_once $base_path_to_learning_resources . 'lib/validate_function.php';
require_once $base_path_to_learning . 'user_management/authenticate.php';
require_once $base_path_to_learning . 'functions/common_functions.php';
//db access function
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
require_once $base_path_to_learning_resources . 'lib/student_birthdays/function.php';

//aws image store
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning . "vendor/autoload.php";

require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';
require_once $base_path_to_learning_resources . 'lib/cron_jobs/student_birthday/student_birthday_mail_template.php';

use Aws\S3\S3Client;
use Aws\S3\Exception\S3Exception;


// $postdata = json_decode(file_get_contents("php://input"), true);

// checkIfNoPhpContentsReceive();

// Get today's date
$today = date('Y-m-d');
// $Date = date('m-d', strtotime($today));  // Getting the month and day only

// // Check if institution_id and branch_id are provided in the input, if not, set them to null
// $institution_id = isset($postdata['institution_id']) ? $postdata['institution_id'] : null;
// $branch_id = isset($postdata['branch_id']) ? $postdata['branch_id'] : null;

$students_data = get_student_birthday($conn_rjllearning, null,$institution_id=null, $branch_id=null, $today,$today);


// Check if the data is an array and contains the 'student_birthdates_info' key
if (is_array($students_data['student_birthdates_info']) and !empty($students_data['student_birthdates_info'])) {
    // Loop through the students and send birthday emails
    foreach ($students_data['student_birthdates_info'] as $student) {

        $to = $student['email'];
        $from = 'no-reply@rejoicelearning.in'; // Set a generic sender email
        $subject = "Happy Birthday " . $student['student_name'];
        $message = "<html><body>";
        $message .= "<h1>Happy Birthday " . $student['student_name'] . "!</h1>";
        $message .= "<p>We wish you a wonderful year ahead!</p>";
        $message .= "</body></html>";
       
        // Sending email via sendMailTemplateForStudentBirthday without institution_id and branch_id
        // sendMailTemplateForStudentBirthday($conn_rjllearning, $from, $to, $subject, $message, $student['workspace_id'], $student['institution_data']['institution_id'], $student['branch_id']['branch_id'], $student['user_id'], $student['institution_data']['logo_path'], $student['class_info']['class_name'], $student['section_info']['section_name'], $student['student_name']);
        
        require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/firebase_notification/fcm_notifications_function.php'; 
        $notificationSent = sendNotification($conn_rjllearning, $student['user_id'], $subject, $message);

        if ($notificationSent) { // If notification is successfully sent
            if (!is_array($to)) {
                $to = explode(',', $to); // Convert comma-separated emails into an array
            }
        
            require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/function/function.php';
            
            updateMailSmsCount(
                $student['workspace_id'], 
                $student['institution_data']['institution_id'], 
                $student['branch_id']['branch_id'], 
                null, 
                null, 
                count($to), 
                'notification', 
                $conn_rjllearning
            );
        }

}
if ( is_array($students_data['faculty_birthdates_info']) and !empty($students_data['faculty_birthdates_info'])) {
    // Loop through the faculty and send birthday emails
    foreach ($students_data['faculty_birthdates_info'] as $faculty) {
        $to = $faculty['email'];
        $from = 'no-reply@rejoicelearning.in'; // Set a generic sender email
        $subject = "Happy Birthday " . $faculty['faculty_name'];
        $message = "<html><body>";
        $message .= "<h1>Happy Birthday " . $faculty['faculty_name'] . "!</h1>";
        $message .= "<p>We wish you a wonderful year ahead!</p>";
        $message .= "</body></html>";

        // Sending email via sendMailTemplateForStaffBirthday
        sendMailTemplateForStaffBirthday(
            $conn_rjllearning, 
            $from, 
            $to, 
            $subject, 
            $message, 
            $faculty['workspace_id'], 
            $faculty['institution_data']['institution_id'], 
            $faculty['branch_info']['branch_id'], 
            $faculty['user_id'], 
            $faculty['institution_data']['logo_path'], 
            $faculty['faculty_name'], 
            $faculty['facultyrole']
            
        );
        
        //  require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/cron_jobs/firebase_notification/fcm_notifications_function.php'; 
        // sendNotification($conn_rjllearning,$faculty['user_id'],$subject, $message);
        // updateMailSmsCount($faculty['workspace_id'], $faculty['institution_data']['institution_id'], $faculty['branch_id']['branch_id'], null, null, count($to), 'notification', $conn_rjllearning);
    }
}
} else {
    return false;
}
