import 'package:userfront/features/dashboard/domain/models.dart';

const headlessServerUserAgentSentinel = '__headless_server__';

bool looksLikeInternalAuditUserAgent(String userAgent) {
  final lower = userAgent.trim().toLowerCase();
  return lower.startsWith('go-http-client/') ||
      lower.startsWith('fasthttp') ||
      lower.startsWith('fiber') ||
      lower.startsWith('undici') ||
      lower.startsWith('node');
}

String preferredAuditLogUserAgent(AuditLogEntry log) {
  final userAgent = log.userAgent.trim();
  final path = log.path.toLowerCase();

  final isHeadlessLinkLog =
      path.contains('/api/v1/auth/magic-link/verify') ||
      path.contains('/api/v1/auth/login/code/verify');
  final isHeadlessPasswordLog = path.contains(
    '/api/v1/auth/headless/password/login',
  );

  if ((isHeadlessLinkLog || isHeadlessPasswordLog) &&
      looksLikeInternalAuditUserAgent(userAgent)) {
    return headlessServerUserAgentSentinel;
  }

  return userAgent;
}
