import Foundation
import Capacitor
import MoEngagePluginBase
import MoEngageSDK

class MoECapacitorCoreUtils {

    static  func getAuthenticationDetailsPayload(from call: CAPPluginCall) -> [String: Any]? {
       guard let appId = call.getString(MoECapacitorConstants.PayloadKeys.appId),
          !appId.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
        return nil
    }
    guard let authData = call.getObject(MoECapacitorConstants.PayloadKeys.authenticationData),
          let authenticationType = authData[MoEngagePluginConstants.Authentication.authType] as? String else {
        return nil
    }

    var dataJson: [String: Any] = [:]

    if authenticationType == MoEngagePluginConstants.Authentication.jwt {
        guard let innerAuthData = authData[MoEngagePluginConstants.General.data] as? [String: Any] else {
            return nil
        }
        dataJson[MoEngagePluginConstants.Authentication.authType] = authenticationType
        dataJson[MoEngagePluginConstants.Authentication.token] = innerAuthData[MoEngagePluginConstants.Authentication.token]
        dataJson[MoEngagePluginConstants.Authentication.userIdentifier] = innerAuthData[MoEngagePluginConstants.Authentication.userIdentifier]
    }
    return [
        MoEngagePluginConstants.General.accountMeta: MoEngagePluginUtils.createAccountPayload(identifier: appId),
        MoEngagePluginConstants.General.data: dataJson
    ]
 }

  static func getAuthenticationErrorPayload(_ payload: [String: Any]) -> [String: Any] {
    guard let accountMeta = payload[MoEngagePluginConstants.General.accountMeta] as? [String: Any],
          let platform = payload[MoEngagePluginConstants.General.platform] as? String,
          let data = payload[MoEngagePluginConstants.General.data] as? [String: Any] else {
        MoEngageLogger.logDefault(
            logLevel: .debug,
            message: "Invalid authentication error payload: missing accountMeta/platform/data"
        )
        return [:]
    }

    guard let authenticationType = data[MoEngagePluginConstants.Authentication.authType] as? String,
          let code = data[MoEngagePluginConstants.Authentication.errorCode] as? String,
          let authenticationType = data[MoEngagePluginConstants.Authentication.authType] as? String,
          let token = data[MoEngagePluginConstants.Authentication.token] as? String,
          let userIdentifier = data[MoEngagePluginConstants.Authentication.userIdentifier] as? String,
          let message = data[MoEngagePluginConstants.Authentication.errorMessage] as? String else {
        MoEngageLogger.logDefault(
            logLevel: .debug,
            message: "Invalid authentication error payload: missing authType/code/token/userIdentifier/message"
        )
        return [:]
    }
    let authData: [String: Any] = [
        MoEngagePluginConstants.Authentication.errorCode: code,
        MoEngagePluginConstants.Authentication.token: token,
        MoEngagePluginConstants.Authentication.userIdentifier: userIdentifier,
        MoEngagePluginConstants.Authentication.errorMessage: message
    ]
    return [
        MoEngagePluginConstants.General.accountMeta: accountMeta,
        MoEngagePluginConstants.General.platform: platform,
        MoEngagePluginConstants.Authentication.authType: authenticationType,
        MoEngagePluginConstants.General.data: authData
    ]
}

}