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

void main() {
  setUpAll(LocaleRegistry.primeWithDefaults);

  group('verification route policy', () {
    test('루트 인증 payload는 전용 verify 라우트로 정리한다', () {
      final redirect = buildDedicatedVerificationRedirect(
        Uri.parse(
          '/?loginId=e2e%40example.com&code=654321&pendingRef=pending-root&utm=drop',
        ),
        localeCode: 'ko',
      );

      expect(
        redirect,
        '/ko/verify?loginId=e2e%40example.com&code=654321&pendingRef=pending-root',
      );
    });

    test('signin 인증 payload도 로그인 화면에서 직접 소비하지 않는다', () {
      final redirect = buildDedicatedVerificationRedirect(
        Uri.parse('/ko/signin?t=magic-token&utm=drop'),
        localeCode: 'ko',
      );

      expect(redirect, '/ko/verify?t=magic-token');
    });

    test('인증 payload 여부를 식별한다', () {
      expect(hasVerificationPayload(Uri.parse('/?t=magic-token')), isTrue);
      expect(
        hasVerificationPayload(
          Uri.parse('/ko/signin?loginId=e2e%40example.com&code=654321'),
        ),
        isTrue,
      );
      expect(
        hasVerificationPayload(Uri.parse('/ko/signin?code=654321')),
        isFalse,
      );
    });

    test('전용 verify 계열 라우트와 완료 라우트 경로를 식별한다', () {
      expect(
        buildLocalizedVerificationCompletePath('ko'),
        '/ko/verify-complete',
      );
      expect(isDedicatedVerificationRoute(Uri.parse('/verification')), isTrue);
      expect(isDedicatedVerificationRoute(Uri.parse('/ko/verify/abc')), isTrue);
      expect(isDedicatedVerificationRoute(Uri.parse('/en/l/SHORT123')), isTrue);
      expect(isDedicatedVerificationRoute(Uri.parse('/ko/signin')), isFalse);
    });

    test('빈 pendingRef와 불완전한 payload는 리다이렉트 query에서 제외한다', () {
      expect(
        buildDedicatedVerificationRedirect(
          Uri.parse(
            '/signin?loginId=user%40example.com&code=123456&pendingRef=',
          ),
          localeCode: 'en',
        ),
        '/en/verify?loginId=user%40example.com&code=123456',
      );
      expect(
        buildDedicatedVerificationRedirect(
          Uri.parse('/signin?loginId=user%40example.com'),
          localeCode: 'en',
        ),
        isNull,
      );
    });

    test('이미 전용 verify 라우트면 다시 리다이렉트하지 않는다', () {
      final redirect = buildDedicatedVerificationRedirect(
        Uri.parse('/ko/verify?loginId=e2e%40example.com&code=654321'),
        localeCode: 'ko',
      );

      expect(redirect, isNull);
    });
  });
}
