<?php

// Set up error reporting
ini_set('display_errors', 1);

// Include necessary files
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_resources . 'includes/specific_db_config.php';

require_once $base_path_to_learning . "vendor/autoload.php";
require_once $base_path_to_learning_resources . 'lib/bucket_functions/bucket_functions.php';
require_once $base_path_to_learning_resources . 'includes/aws_bucket_config.php';
require_once $base_path_to_learning_resources . 'lib/task_schedule/task_scheduler_functions.php';


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


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


$required_fields = ['assignment_id', 'api_signature', 'function_type', 'jwt_token'];
foreach ($required_fields as $field) {
    if (!isset($postdata[$field]) || !checkIfIssetAndNotEmpty($postdata[$field])) {
        errorreturn("Missing required field: $field");
    }
}

validateApiSignature($postdata['api_signature']);

// Authenticate user and get user details
$jwttoken = $postdata['jwt_token'];
$jwt_validate = authenticate_jwt($jwttoken);
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$username = $jwt_validate->data->username;
$role = $jwt_validate->data->role;
$branch_id = $jwt_validate->data->branch_id;
$institution_id = $jwt_validate->data->institution_id;

// Validate user access to specific database
$specific_db_name = validateUserAccess($conn_rjllearning, $userid, $role, $workspace_id,["principal"], null);
if (!$specific_db_name) {
    errorreturn('Access denied.');
} else {
    $conn_rjllearning_specific = dynamicDbConnection($specific_db_name);
}
// Include necessary function files
require_once $base_path_to_learning . 'functions/common_functions.php';
require_once $base_path_to_learning . 'functions/get_branch_functions.php';
require_once $base_path_to_learning . 'user_management/user_versioning.php';

$version = isset($_POST['version']) ? $_POST['version'] : (isset($postdata['version']) ? $postdata['version'] : null);
$updated_time = isset($_POST['updated_time']) ? $_POST['updated_time'] : (isset($postdata['updated_time']) ? $postdata['updated_time'] : null);

$response = user_data_refresh($conn_rjllearning, $conn_rjllearning_specific, $version, $updated_time, $userid, $institution_id);

// Validate basic parameters
$function_type = trim($postdata['function_type']);

$academic_id = CheckAcademicSession($conn_rjllearning_specific, $institution_id);
if ($academic_id == false) {
    errorreturn("Access denied. Academic session not active.");
}

if ($function_type == 'delete_assignment') {
    
    if (empty($postdata['assignment_id'])) {
        errorreturn("assignment_id is required for deletion.");
    }

    $assignment_id = $postdata['assignment_id'];

    
    $check_assignment_sql = "SELECT id FROM assignments WHERE id = ? AND institution_id = ? AND branch_id = ?";
    $stmt = $conn_rjllearning_specific->prepare($check_assignment_sql);
    $stmt->execute([$assignment_id, $institution_id, $branch_id]);

    $existing_assignment = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($existing_assignment) {
        
        $delete_result = delete_Assignment($assignment_id, $conn_rjllearning_specific, $s3_learning_bucket, $bucket_name, $institution_id, $branch_id);
        
        manageSchedulerTask($conn_rjllearning, $workspace_id, $institution_id, 'assignment', $assignment_id, null, null, 1, $process_type = 'delete');
        
        successreturn("Successfully Deleted Assignment.");
    } else {
        errorreturn("Assignment ID does not exist.");
    }
}



