package auth

import (
	"strings"

	authdomain "gateway/internal/model/auth/domain"
)

func oauthState(prefix, sessionID string) string {
	return prefix + sessionID
}

func parseOAuthState(state, prefix string) (sessionID string, err error) {
	state = strings.TrimSpace(state)
	if !strings.HasPrefix(state, prefix) {
		return "", errb.InputInvalidFormat("invalid oauth state")
	}
	sessionID = strings.TrimPrefix(state, prefix)
	if sessionID == "" {
		return "", errb.InputInvalidFormat("invalid oauth state")
	}
	return sessionID, nil
}

func registerOAuthState(sessionID string) string {
	return oauthState(authdomain.OAuthStatePrefixRegister, sessionID)
}

func loginOAuthState(sessionID string) string {
	return oauthState(authdomain.OAuthStatePrefixLogin, sessionID)
}

func parseRegisterOAuthState(state string) (string, error) {
	return parseOAuthState(state, authdomain.OAuthStatePrefixRegister)
}

func parseLoginOAuthState(state string) (string, error) {
	return parseOAuthState(state, authdomain.OAuthStatePrefixLogin)
}
