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

AuditLogEntry _log({
  required String eventType,
  String userAgent = '',
  Map<String, dynamic>? details,
}) {
  return AuditLogEntry.fromJson({
    'event_id': 'audit-1',
    'timestamp': '2026-04-14T00:00:00Z',
    'user_id': 'user-123',
    'event_type': eventType,
    'status': 'success',
    'user_agent': userAgent,
    'details': details == null ? '' : details.toString(),
  });
}

void main() {
  test('headless link login maps internal client user agent to sentinel', () {
    final log = AuditLogEntry.fromJson({
      'event_id': 'audit-1',
      'timestamp': '2026-04-14T00:00:00Z',
      'user_id': 'user-123',
      'event_type': 'POST /api/v1/auth/login/code/verify',
      'status': 'success',
      'user_agent': 'undici',
      'details':
          '{"approved_user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/146.0.0.0 Safari/537.36"}',
    });

    expect(preferredAuditLogUserAgent(log), headlessServerUserAgentSentinel);
  });

  test(
    'headless password login maps internal client user agent to sentinel',
    () {
      final log = _log(
        eventType: 'POST /api/v1/auth/headless/password/login',
        userAgent: 'undici',
      );

      expect(preferredAuditLogUserAgent(log), headlessServerUserAgentSentinel);
    },
  );

  test('non-headless login preserves original browser user agent', () {
    const browserUa =
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/146.0.0.0 Safari/537.36';
    final log = _log(
      eventType: 'POST /api/v1/auth/password/login',
      userAgent: browserUa,
    );

    expect(preferredAuditLogUserAgent(log), browserUa);
  });
}
