import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/core/i18n/locale_registry.dart';
import 'package:userfront/core/i18n/locale_utils.dart';

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

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

  group('locale_utils', () {
    test('normalizeLocaleCode handles supported locales', () {
      expect(normalizeLocaleCode('ko'), 'ko');
      expect(normalizeLocaleCode('ko-KR'), 'ko');
      expect(normalizeLocaleCode('en'), 'en');
      expect(normalizeLocaleCode('en-US'), 'en');
    });

    test('normalizeLocaleCode falls back to default', () {
      expect(normalizeLocaleCode('ja'), defaultLocaleCode);
      expect(normalizeLocaleCode(null), defaultLocaleCode);
      expect(normalizeLocaleCode(''), defaultLocaleCode);
    });

    test('extractLocaleFromPath picks locale when present', () {
      expect(extractLocaleFromPath(Uri.parse('/ko/signin')), 'ko');
      expect(extractLocaleFromPath(Uri.parse('/en/profile')), 'en');
      expect(extractLocaleFromPath(Uri.parse('/ko')), 'ko');
    });

    test('extractLocaleFromPath returns null when missing', () {
      expect(extractLocaleFromPath(Uri.parse('/signin')), isNull);
      expect(extractLocaleFromPath(Uri.parse('/zz/signin')), isNull);
    });

    test('stripLocalePath removes locale segment', () {
      expect(stripLocalePath(Uri.parse('/ko/signin')), '/signin');
      expect(stripLocalePath(Uri.parse('/en/profile')), '/profile');
      expect(stripLocalePath(Uri.parse('/ko')), '/');
      expect(stripLocalePath(Uri.parse('/en/')), '/');
    });

    test('stripLocalePath keeps path without locale', () {
      expect(stripLocalePath(Uri.parse('/signin')), '/signin');
      expect(stripLocalePath(Uri.parse('/auth/callback')), '/auth/callback');
    });

    test('buildLocalizedPath applies locale', () {
      expect(buildLocalizedPath('ko', Uri.parse('/signin')), '/ko/signin');
      expect(buildLocalizedPath('en', Uri.parse('/signin')), '/en/signin');
      expect(buildLocalizedPath('ko', Uri.parse('/')), '/ko');
      expect(buildLocalizedPath('en', Uri.parse('/')), '/en');
    });

    test('buildLocalizedPath preserves query parameters', () {
      final uri = Uri.parse('/signin?redirect_uri=https://example.com');
      expect(
        buildLocalizedPath('ko', uri),
        '/ko/signin?redirect_uri=https://example.com',
      );
    });

    test('buildLocalizedPath preserves redirect_url parameter', () {
      final uri = Uri.parse('/signin?redirect_url=https://example.com/after');
      expect(
        buildLocalizedPath('ko', uri),
        '/ko/signin?redirect_url=https://example.com/after',
      );
    });

    test('buildLocalizedPath preserves raw query order and duplicates', () {
      final uri = Uri.parse(
        '/signin?a=1&a=2&redirect_uri=https%3A%2F%2Fexample.com%2Fcb%3Fx%3D1%26y%3D2',
      );
      expect(
        buildLocalizedPath('ko', uri),
        '/ko/signin?a=1&a=2&redirect_uri=https%3A%2F%2Fexample.com%2Fcb%3Fx%3D1%26y%3D2',
      );
    });

    test('buildLocalizedPath preserves fragment', () {
      final uri = Uri.parse('/signin?notice=qr_login_required#auth');
      expect(
        buildLocalizedPath('ko', uri),
        '/ko/signin?notice=qr_login_required#auth',
      );
    });

    test('buildLocalizedPath replaces existing locale', () {
      expect(buildLocalizedPath('en', Uri.parse('/ko/signin')), '/en/signin');
      expect(buildLocalizedPath('ko', Uri.parse('/en/profile')), '/ko/profile');
    });

    test('buildLocalizedPath keeps unknown 2-letter prefix as path', () {
      expect(
        buildLocalizedPath('ko', Uri.parse('/zz/signin')),
        '/ko/zz/signin',
      );
    });

    test('buildSigninRedirectPath keeps path without query', () {
      expect(
        buildSigninRedirectPath('ko', Uri.parse('/ko/profile')),
        '/ko/signin',
      );
    });

    test('buildSigninRedirectPath preserves full raw query', () {
      final uri = Uri.parse(
        '/ko/profile?a=1&a=2&redirect_uri=https%3A%2F%2Fexample.com%2Fcb%3Fx%3D1%26y%3D2&notice=qr_login_required',
      );
      expect(
        buildSigninRedirectPath('ko', uri),
        '/ko/signin?a=1&a=2&redirect_uri=https%3A%2F%2Fexample.com%2Fcb%3Fx%3D1%26y%3D2&notice=qr_login_required',
      );
    });

    test('buildSigninRedirectPath preserves redirect_url and redirect_uri', () {
      final uri = Uri.parse(
        '/ko/profile?redirect_url=https%3A%2F%2Fa.example.com%2Fcb&redirect_uri=https%3A%2F%2Fb.example.com%2Fcb',
      );
      expect(
        buildSigninRedirectPath('ko', uri),
        '/ko/signin?redirect_url=https%3A%2F%2Fa.example.com%2Fcb&redirect_uri=https%3A%2F%2Fb.example.com%2Fcb',
      );
    });

    test('buildLocalizedHomePath keeps locale from uri', () {
      expect(buildLocalizedHomePath(Uri.parse('/ko/signin')), '/ko/dashboard');
      expect(buildLocalizedHomePath(Uri.parse('/en/profile')), '/en/dashboard');
    });

    test('buildLocalizedHomePath falls back to preferred locale', () {
      expect(
        buildLocalizedHomePath(Uri.parse('/signin'), preferredLocaleCode: 'ko'),
        '/ko/dashboard',
      );
    });

    test('buildLocalizedSigninPath keeps locale from uri', () {
      expect(buildLocalizedSigninPath(Uri.parse('/ko')), '/ko/signin');
      expect(buildLocalizedSigninPath(Uri.parse('/en/profile')), '/en/signin');
    });

    test('buildLocalizedSigninPath falls back to preferred locale', () {
      expect(
        buildLocalizedSigninPath(
          Uri.parse('/profile'),
          preferredLocaleCode: 'ko',
        ),
        '/ko/signin',
      );
    });
  });
}
