package shared import ( "fmt" "galaxy/authsession/internal/domain/sessionlimit" "galaxy/authsession/internal/ports" ) // EvaluateSessionLimit evaluates the Stage-4 active-session creation decision // from the loaded configuration and current active-session count. func EvaluateSessionLimit(config ports.SessionLimitConfig, activeSessionCount int) (sessionlimit.Decision, error) { if err := config.Validate(); err != nil { return sessionlimit.Decision{}, InternalError(fmt.Errorf("evaluate session limit: %w", err)) } if activeSessionCount < 0 { return sessionlimit.Decision{}, InternalError(fmt.Errorf("evaluate session limit: active session count %d is negative", activeSessionCount)) } decision := sessionlimit.Decision{ ActiveSessionCount: activeSessionCount, NextSessionCount: activeSessionCount + 1, } if config.ActiveSessionLimit == nil { decision.Kind = sessionlimit.KindDisabled } else { decision.ConfiguredLimit = config.ActiveSessionLimit if decision.NextSessionCount <= *config.ActiveSessionLimit { decision.Kind = sessionlimit.KindAllowed } else { decision.Kind = sessionlimit.KindExceeded } } if err := decision.Validate(); err != nil { return sessionlimit.Decision{}, InternalError(fmt.Errorf("evaluate session limit: %w", err)) } return decision, nil }