<?php
ini_set('display_errors', 1);

require_once '/home/rejoicebilldesk/public_html/rjllearning_development/resources/base_path_include.php';

require_once $GLOBALS['base_path_to_learning_resources']  . "includes/connect.php"; // use require for mandatory files
require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/function.php';
require_once $GLOBALS['base_path_to_learning_resources'] . 'lib/validate_api_signature.php';
require_once $GLOBALS['base_path_to_learning_resources'] .'external_requests/learning_solutions_finware/curl_sms_mail_counter_solutions.php';

function fetchRecordById($conn, $table, $id, $institution_id, $status = 1) {
    $sql = "SELECT * FROM $table WHERE id = ? AND institution_id = ? ";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$id, $institution_id]);
    return $stmt->fetch(PDO::FETCH_ASSOC);
}

                            
// function updateMailSmsCount($workspace_id, $institution_id, $branch_id, $mails_sent, $sms_sent, $notifications_sent, $type, $conn_rjllearning)
// {
//     // Ensure $mails_sent, $sms_sent, and $notifications_sent are integers
//     $mails_sent = is_numeric($mails_sent) ? (int)$mails_sent : 0;
//     $sms_sent = is_numeric($sms_sent) ? (int)$sms_sent : 0;
//     $notifications_sent = is_numeric($notifications_sent) ? (int)$notifications_sent : 0;
   
//     // Debug print for mails_sent
//     // print_r("Mails sent: " . $mails_sent . "\n");

//     // Get the current date and time
//     $current_date = date('Y-m-d');
//     $current_time = date('H:i:s');
    
//     // Generate a unique ID based on workspace_id, institution_id, branch_id, and current date
//     $unique_id = "{$workspace_id}/{$institution_id}/{$branch_id}/{$current_date}";
//     // print_R($unique_id);
    
//     // Prepare the column to update and the corresponding value based on the $type
//     $column = '';
//     $value = 0;
//     $last_date_column = '';
//     $last_time_column = '';

//     // Prepare the query dynamically based on the type
//     if ($type === 'mail') {
//         $column = 'mails_sent';
//         $value = $mails_sent;
//         $last_date_column = 'last_mail_date';
//         $last_time_column = 'last_mail_time';
//     } elseif ($type === 'sms') {
//         $column = 'sms_sent';
//         $value = $sms_sent;
//         $last_date_column = 'last_sms_date';
//         $last_time_column = 'last_sms_time';
//     } elseif ($type === 'notification') {
//         $column = 'notification_sent';
//         $value = $notifications_sent;
//         $last_date_column = 'last_notification_date';
//         $last_time_column = 'last_notification_time';
//     } else {
//         return "Invalid type. Please use 'mail', 'sms', or 'notification'.";
//     }

//     // Debug print for the column and type
//     // print_r("Column: " . $column . "\n");
//     // print_r("Type: " . $type . "\n");

//     // Prepare query dynamically based on the type and only include relevant columns
//     // Use IFNULL to handle NULL values and treat them as 0
//     $query = "INSERT INTO mail_sms_counter (workspace_id, institution_id, branch_id, unique_id, status) 
//               VALUES (?, ?, ?, ?, ?)
//               ON DUPLICATE KEY UPDATE 
//               $column = IFNULL($column, 0) + ?, 
//               $last_date_column = ?, 
//               $last_time_column = ?, 
//               status = ?";

//     // Debug print for the query
//     // print_r("Query: " . $query . "\n");

//     // Prepare and execute the query only for the relevant field (mails_sent, sms_sent, or notification_sent)
//     $stmt = $conn_rjllearning->prepare($query);
//     $result = $stmt->execute([
//         $workspace_id, 
//         $institution_id, 
//         $branch_id, 
//         $unique_id, 
//         1,  // status
//         $value,  // Increment by the value of mails/sms/notifications sent
//         $current_date, 
//         $current_time, 
//         1  // status
//     ]);

//     // Debug print for execution result
//     if ($result) {
//         // print_r("Query executed successfully.\n");
//         return true;
//     } else {
//         // print_r("Query execution failed.\n");
//         return false;
//     }

//     return true;
// }

function updateMailSmsCount($workspace_id, $institution_id, $branch_id, $mails_sent, $sms_sent, $notifications_sent, $type, $conn_rjllearning)
{
    
    
     require_once $GLOBALS['base_path_to_learning_resources'] .'external_requests/learning_solutions_finware/curl_sms_mail_counter_solutions.php';
    // Ensure $mails_sent, $sms_sent, and $notifications_sent are integers
    $mails_sent = is_numeric($mails_sent) ? (int)$mails_sent : 0;
    $sms_sent = is_numeric($sms_sent) ? (int)$sms_sent : 0;
    $notifications_sent = is_numeric($notifications_sent) ? (int)$notifications_sent : 0;

    // Get the current date and time
    $current_date = date('Y-m-d');
    $current_time = date('H:i:s');

    // Generate a unique ID based on workspace_id, institution_id, branch_id, and current date
    $unique_id = "{$workspace_id}/{$institution_id}/{$branch_id}/{$current_date}";

    // Prepare the column to update and the corresponding value based on the $type
    $column = '';
    $value = 0;
    $last_date_column = '';
    $last_time_column = '';

    if ($type === 'mail') {
        $column = 'mails_sent';
        $value = $mails_sent;
        $last_date_column = 'last_mail_date';
        $last_time_column = 'last_mail_time';
    } elseif ($type === 'sms') {
        $column = 'sms_sent';
        $value = $sms_sent;
        $last_date_column = 'last_sms_date';
        $last_time_column = 'last_sms_time';
    } elseif ($type === 'notification') {
        $column = 'notification_sent';
        $value = $notifications_sent;
        $last_date_column = 'last_notification_date';
        $last_time_column = 'last_notification_time';
    } else {
        return "Invalid type. Please use 'mail', 'sms', or 'notification'.";
    }

    // Insert or update query to handle both new inserts and updates
    $query = "
        INSERT INTO mail_sms_counter (workspace_id, institution_id, branch_id, unique_id, $column, $last_date_column, $last_time_column, status)
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)
        ON DUPLICATE KEY UPDATE 
        $column = IFNULL($column, 0) + ?, 
        $last_date_column = ?, 
        $last_time_column = ?, 
        status = ?
    ";

    // Prepare and execute the query
    $stmt = $conn_rjllearning->prepare($query);
    $result = $stmt->execute([
        $workspace_id, 
        $institution_id, 
        $branch_id, 
        $unique_id, 
        $value,  // Initial value for the respective column
        $current_date, 
        $current_time, 
        1,  // status
        $value,  // Increment value for the update
        $current_date, 
        $current_time, 
        1  // status
    ]);
   
    if ($result) {
        
        
        $data = [
            "workspace_id" => $workspace_id,
            "product_id" => 2,
            "unique_id" => $unique_id,
            "mails_sent" => $mails_sent,
            "sms_sent" => $sms_sent,
            "type" => $type,
            "status" => 1
        ];
        // curlWorkspaceSmsMailCounterSolutions($data);
        return true;
    } else {
        return false;
    }
    
    
}












