<?php
ini_set('display_errors', 1);
// this file will get results based from user_uploads as filtered by user to narrow down aand get a list of documents.

require_once '../../../resources/base_path_include.php';

//valdation file
require_once $base_path_to_resources . "includes/config.php"; // use require for mandatory files
require_once $base_path_to_resources . 'lib/function.php';
require_once $base_path_to_resources . 'lib/validate_api_signature.php';
require_once $base_path_to_resources . 'lib/validate_function.php';
require_once $base_path . 'user_management/authenticate.php';

//function file
require_once $base_path . 'modules/accounting/book_management/basic_book_info_function.php';

//user upload
require_once $base_path_to_resources . 'lib/bucket_functions/bucket_functions.php';

//aws image store
require_once $base_path . "vendor/autoload.php";

require_once $base_path_to_resources . 'includes/aws_bucket_config.php';


//creating dynamic db
require_once $base_path_to_resources . 'includes/specific_db_config.php';

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

$postdata = json_decode(file_get_contents("php://input"), true);
checkIfNoPhpContentsReceive();
validateApiSignature($postdata['api_signature']);

//authenticate jwt token
$jwttoken = $postdata['jwt_token'];
$company_id=$postdata['company_id'];
$branch_id=$postdata['branch_id'];
$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is decoded in data of jwt use from there
$userid = $jwt_validate->data->id;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;
$created_by = $jwt_validate->data->username;

// mandatory function type should be upload_to_my_files
if (isset($postdata['function_type']) and !checkIfIssetAndNotEmpty($postdata['function_type']) or $postdata['function_type'] != 'delete_drive_item' and isset($postdata['user_upload_id']) and !empty($postdata['user_upload_id'])) {
    errorreturn('Access denied.');
}
$function_code = $postdata['function_type'];
$confirmed_role_user = validatePrimaryAccess($conn, $workspace_id, $role, $userid, ['superadmin', 'admin', null]);
if (!$confirmed_role_user){ 
        errorreturn('Access denied.');
}else{
        $specific_db_conn = dynamicDbConnection($confirmed_role_user);
         if ($role == null) {
            checkCompanyBranchFunctionAccess($specific_db_conn, $role, $userid, $workspace_id, null, null, null,  $function_code);
         }
}

$user_upload_id = $postdata['user_upload_id'];
// should either be owned by user and be private or public
$sql = "Select share_mode, share_with, let_others_delete, user_id, path from user_upload where workspace_id = ? and id = ?";
$fetch_file = $specific_db_conn->prepare($sql);
$fetch_file->execute(array($workspace_id, $user_upload_id));
$result_file = $fetch_file->fetchAll();
if (count($result_file) != 1) {
    errorreturn('Unable to delete file. Err. 1');
}
$share_mode = $result_file[0]['share_mode'];
$share_with = $result_file[0]['share_with'];
if ($share_with == null or $share_with == '') {
    $share_with = [];
} else {
    json_decode($share_with, true);
}
$let_others_delete = $result_file[0]['let_others_delete'];
$user_id = $result_file[0]['user_id'];
$path = $result_file[0]['path'];

$delete_bool = 0;

if ($role == 'superadmin' or $role == 'admin') {
    // delete it
    $delete_bool = 1;
} else {
    if ($share_mode == 'public' and $let_others_delete == true) {
        // then allow to delete
        $delete_bool = 1;
    } else if ($share_mode == 'private' and $user_id == $userid) {
        // then allow to delete
        $delete_bool = 1;
    } else if ($share_mode == 'protected' and ($user_id == $userid or in_array($created_by, $share_with))) {
        // then allow to delete
        $delete_bool = 1;
    }
}
if ($delete_bool != 1) {
    errorreturn('Access denied to delete this file.');
} else {
    awsS3UserUploadDelete($s3_finware_bucket, $finware_bucketName, $specific_db_conn, $workspace_id, $path, $user_upload_id, true);
    successreturn(array('status' => 1));
}

// or if in shared then user then file should have let others delete option
