package server.logic.ws_protocol.JSON.handlers.auth;

import server.logic.ws_protocol.JSON.utils.AuthKeyUtils;

import java.util.Base64;

final class AuthSessionTypeSupport {

    static final int SESSION_TYPE_CLIENT = 1;
    static final int SESSION_TYPE_WALLET = 50;
    static final int SESSION_TYPE_HOMESERVER = 100;
    static final int SESSION_TYPE_MISMATCH_STATUS = 460;

    private AuthSessionTypeSupport() {}

    static int normalizeRequestedSessionType(Integer rawType) {
        return rawType == null ? SESSION_TYPE_CLIENT : rawType.intValue();
    }

    static boolean isSupportedSessionType(int sessionType) {
        return sessionType == SESSION_TYPE_CLIENT
                || sessionType == SESSION_TYPE_WALLET
                || sessionType == SESSION_TYPE_HOMESERVER;
    }

    static String normalizeClientPlatform(String clientPlatform) {
        if (clientPlatform == null) return "";
        String trimmed = clientPlatform.trim();
        if (trimmed.length() <= 64) return trimmed;
        return trimmed.substring(0, 64);
    }

    static byte[] tryParseSessionPublicKey32(String sessionKeyApi) {
        if (sessionKeyApi == null || sessionKeyApi.isBlank()) return null;
        try {
            return AuthKeyUtils.parseEd25519PublicKey(sessionKeyApi, "sessionKey");
        } catch (Exception ignored) {
            try {
                byte[] raw = Base64.getDecoder().decode(sessionKeyApi.trim());
                return raw.length == 32 ? raw : null;
            } catch (Exception ignoredToo) {
                return null;
            }
        }
    }
}
