import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/core/i18n/locale_registry.dart';
import 'package:userfront/features/auth/domain/login_link_route_policy.dart';

void main() {
  group('login_link_route_policy', () {
    setUp(() {
      LocaleRegistry.setSupportedLocaleCodesForTest(['ko', 'en']);
    });

    tearDown(() {
      LocaleRegistry.resetForTest();
    });

    test('extracts short code from plain short-code route', () {
      final shortCode = extractLoginShortCode(Uri.parse('/l/AB123456'));
      expect(shortCode, 'AB123456');
    });

    test('extracts short code from localized short-code route', () {
      final shortCode = extractLoginShortCode(Uri.parse('/ko/l/AB123456'));
      expect(shortCode, 'AB123456');
    });

    test('treats localized short-code route as public path', () {
      final isPublic = isPublicAuthPath(
        '/l/AB123456',
        Uri.parse('/ko/l/AB123456'),
      );
      expect(isPublic, isTrue);
    });

    test('public auth path 목록을 허용하고 일반 보호 경로는 제외한다', () {
      final publicPaths = [
        '/signin',
        '/signup',
        '/login',
        '/registration',
        '/verify',
        '/verification',
        '/verify-complete',
        '/verify/token',
        '/l/AB123456',
        '/approve',
        '/ql/AB123456',
        '/forgot-password',
        '/recovery',
        '/reset-password',
        '/error',
        '/settings',
        '/consent',
        '/consent/challenge',
      ];

      for (final path in publicPaths) {
        expect(isPublicAuthPath(path, Uri.parse(path)), isTrue, reason: path);
      }

      expect(
        isPublicAuthPath('/dashboard', Uri.parse('/ko/dashboard')),
        isFalse,
      );
      expect(
        isPublicAuthPath('/dashboard', Uri.parse('/ko/auth/consent/callback')),
        isTrue,
      );
    });

    test('short code route가 아니면 null을 반환한다', () {
      expect(extractLoginShortCode(Uri.parse('/login')), isNull);
      expect(extractLoginShortCode(Uri.parse('/l')), isNull);
      expect(extractLoginShortCode(Uri.parse('/x/AB123456')), isNull);
    });
  });
}
