//
//  UserClient.swift
//  
//
//  Created by TB_COWORKER on 13/06/2023.
//

import Foundation
import TrustBuilderMfaLibSwift

public class UserClient {

    public func lockSecureToken(secureToken: SecureTokenDTO, bearerToken: String, tknAliasToLock: String)
    async throws -> ApiResponse<BoolResponseEntity, LockSecureTokenErrors> {
        let clientApi = APIClient(baseURL: secureToken.serverUrl, defaultHeaders: ["Authorization": "Bearer " + bearerToken])
        let request = API.SecureTokens.UsrLockSecureToken.Request(tknAlias: tknAliasToLock)
        return try await UserApiResponseParser.parseResponse(request: request, clientApi: clientApi, BoolResponseLockSecureTokenErrorsApiResponseEntity.self)
    }
    
    public func unlockSecureToken(secureToken: SecureTokenDTO, bearerToken: String, tknAliasToUnlock: String)
    async throws -> ApiResponse<BoolResponseEntity, UnlockSecureTokenErrors> {
        let clientApi = APIClient(baseURL: secureToken.serverUrl, defaultHeaders: ["Authorization": "Bearer " + bearerToken])
        let request = API.SecureTokens.UsrUnlockSecureToken.Request(tknAlias: tknAliasToUnlock)
        return try await UserApiResponseParser.parseResponse(request: request, clientApi: clientApi, BoolResponseUnlockSecureTokenErrorsApiResponseEntity.self)
    }
    
    public func deleteSecureToken(secureToken: SecureTokenDTO, bearerToken: String, tknAliasToDelete: String)
    async throws -> ApiResponse<BoolResponseEntity, DeleteSecureTokenErrors> {
        let clientApi = APIClient(baseURL: secureToken.serverUrl, defaultHeaders: ["Authorization": "Bearer " + bearerToken])
        let request = API.SecureTokens.UsrDeleteSecureToken.Request(tknAlias: tknAliasToDelete)
        return try await UserApiResponseParser.parseResponse(request: request, clientApi: clientApi, BoolResponseDeleteSecureTokenErrorsApiResponseEntity.self)
    }
    
    public func getSecureTokens(secureToken: SecureTokenDTO, bearerToken: String)
    async throws -> ApiResponse<SecureTokenDtoPaginatedListEntity, SecureTokensListingErrors> {
        let clientApi = APIClient(baseURL: secureToken.serverUrl, defaultHeaders: ["Authorization": "Bearer " + bearerToken])
        let request = API.SecureTokens.UsrGetSecureTokens.Request()
        return try await UserApiResponseParser.parseResponse(request: request, clientApi: clientApi, SecureTokenDtoPaginatedListEntity.self, listing: true)
    }
}
