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

require_once '../../../resources/base_path_include.php';
require_once $base_path_to_learning_resources . "includes/connect.php";
require_once $base_path_to_learning_resources . 'lib/function.php';
require_once $base_path_to_learning_resources . 'lib/validate_api_signature.php';
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';
require_once $base_path_to_learning_resources . 'includes/specific_db_config.php';
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';


use GeminiAPI\Client;
use GeminiAPI\Resources\Parts\TextPart;

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

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

checkIfNoPhpContentsReceive();

if (
    
    
    !isset($postdata['api_signature']) || !checkIfIssetAndNotEmpty($postdata['api_signature']) ||
    !isset($postdata['function_type']) || !checkIfIssetAndNotEmpty($postdata['function_type']) ||
    !isset($postdata['jwt_token']) || !checkIfIssetAndNotEmpty($postdata['jwt_token'])
) {
    errorreturn("All * fields are required.");
}

validateApiSignature($postdata['api_signature']);

$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);

if (!$jwt_validate) {
    errorreturn("Invalid JWT token.");
}

$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;



$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id, ["superadmin","admin","principal","teacher", "student"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
}

$conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
if (!$conn_rjllearning_specific) {
    errorreturn('Failed to connect to the specific database.');
}



if ($postdata['function_type'] == "update_status") {
    if (!isset($postdata['ai_id']) || empty($postdata['ai_id'])) {
        errorreturn("ai_id is required.");
    }

    $ids = $postdata['ai_id']; // Get ai_id from request

    if ($ids === "all") {
        // If "all" is passed, update all records
        $sql = "UPDATE ai_responses SET status = 0";
        $stmt = $conn_rjllearning->prepare($sql);
        $result = $stmt->execute();
    } elseif (is_array($ids) && !empty($ids)) {
        // If specific IDs are provided as an array, update only those records
        $placeholders = implode(',', array_fill(0, count($ids), '?'));
        $sql = "UPDATE ai_responses SET status = 0 WHERE id IN ($placeholders)";
        $stmt = $conn_rjllearning->prepare($sql);
        $result = $stmt->execute($ids);
    } else {
        errorreturn("Invalid ai_id. It should be 'all' or an array of IDs.");
    }

    // If the update was successful, return a success message
    if ($result) {
        successreturn("Status updated successfully.");
    } else {
        errorreturn("Failed to update status.");
    }
}

?>
