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

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';

//creating dynamic db
require_once $base_path_to_resources . 'includes/specific_db_config.php';
$postdata = json_decode(file_get_contents("php://input"), true);

checkIfNoPhpContentsReceive();


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

//authenticate jwt token
$jwttoken = $postdata['jwt_token'];

$jwt_validate = authenticate_jwt($jwttoken);
//then workspace_id is iin decodede data of jwt use from there
$created_by = $jwt_validate->data->username;
$workspace_id = $jwt_validate->data->workspace_id;
$role = $jwt_validate->data->role;
$userid = $jwt_validate->data->id;

function deleteCatgory($specific_db_conn, $category_id, $workspace_id)
{
    
    
     $sql = "SELECT name FROM upload_categories WHERE id = ? AND workspace_id = ?";
    $fetch_category = $specific_db_conn->prepare($sql);
    $fetch_category->execute(array($category_id, $workspace_id));
    $result_category = $fetch_category->fetchAll();
    
    if (count($result_category) == 1) {
        $name = $result_category[0]['name'];
        if ($result_category[0]['name'] === 'Others') {
            errorreturn('Contact admin');
        }
    }
    
    $sql = "DELETE FROM upload_categories WHERE id = ? and workspace_id =?";
    $stmt = $specific_db_conn->prepare($sql);
    $stmt->execute(array($category_id, $workspace_id));
    $count = $stmt->rowCount();
    if ($count == 1) {
        return "Delete catgory Successfully";
    } else {
        errorreturn("unable to delete category");
    }
}

if ($postdata['function_type'] == 'delete_category') {
    
      $category_id = trim($postdata['category_id']);
    
      $function_code = 'delete_category';
      $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);
               }
      }
      $success['data'] = deleteCatgory($specific_db_conn, $category_id, $workspace_id);
      successreturn($success);
      
}

