import 'package:flutter_test/flutter_test.dart';
import 'package:userfront/features/auth/domain/login_challenge_resolver.dart';

void main() {
  group('login_challenge_resolver', () {
    test('widget 값이 있으면 최우선으로 사용', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: 'widget-challenge',
        uri: Uri.parse('/ko/login'),
        rawSearch: '?login_challenge=raw-search',
        rawHref: 'https://sso.example.test/ko/login?login_challenge=raw-href',
      );

      expect(resolved.value, 'widget-challenge');
      expect(resolved.source, LoginChallengeSource.widget);
    });

    test('widget 값이 없으면 URI query에서 복구', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: null,
        uri: Uri.parse('/ko/login?login_challenge=uri-query'),
        rawSearch: '',
        rawHref: '',
      );

      expect(resolved.value, 'uri-query');
      expect(resolved.source, LoginChallengeSource.uriQuery);
    });

    test('URI query가 비어 있으면 raw search에서 복구', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: null,
        uri: Uri.parse('/ko/login'),
        rawSearch: '?login_challenge=raw-search-value&x=1',
        rawHref: '',
      );

      expect(resolved.value, 'raw-search-value');
      expect(resolved.source, LoginChallengeSource.rawSearch);
      expect(resolved.rawSearchHasLoginChallenge, isTrue);
    });

    test('raw search도 비어 있으면 raw href에서 복구', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: null,
        uri: Uri.parse('/ko/login'),
        rawSearch: '',
        rawHref:
            'https://sso.example.test/ko/login?a=1&login_challenge=raw-href-value#fragment',
      );

      expect(resolved.value, 'raw-href-value');
      expect(resolved.source, LoginChallengeSource.rawHref);
      expect(resolved.rawHrefHasLoginChallenge, isTrue);
    });

    test('raw query 파싱 실패 시 수동 파싱으로 복구하고 diagnostics를 남긴다', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: '  ',
        uri: Uri.parse('/ko/login'),
        rawSearch: '?x=%E0%A4%A&login_challenge=manual%20value',
        rawHref: '',
      );

      expect(resolved.value, 'manual value');
      expect(resolved.source, LoginChallengeSource.rawSearch);
      expect(resolved.toDiagnostics(), {
        'resolved_value_len': 12,
        'resolved_source': 'rawSearch',
        'uri_has_login_challenge': false,
        'raw_search_has_login_challenge': true,
        'raw_href_has_login_challenge': false,
      });
    });

    test('raw href가 일반 URI로 파싱되지 않아도 query 조각에서 복구한다', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: null,
        uri: Uri.parse('/ko/login'),
        rawSearch: '?login_challenge',
        rawHref: 'not a url ?login_challenge=href%20fallback#fragment',
      );

      expect(resolved.value, 'href fallback');
      expect(resolved.source, LoginChallengeSource.rawHref);
    });

    test('값이 전부 없으면 missing', () {
      final resolved = resolveLoginChallenge(
        widgetLoginChallenge: null,
        uri: Uri.parse('/ko/login'),
        rawSearch: '',
        rawHref: 'https://sso.example.test/ko/login?x=1',
      );

      expect(resolved.value, isNull);
      expect(resolved.source, LoginChallengeSource.missing);
    });
  });
}
