import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';

import 'package:tdc114plus/main.dart' as app;

const _assumeLoggedIn = bool.fromEnvironment('TDC114_SMOKE_ASSUME_LOGGED_IN');

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets('shows Baron SSO login screen', (tester) async {
    app.main();
    await tester.pumpAndSettle();

    if (_assumeLoggedIn) {
      return;
    }

    expect(find.text('Baron SSO 로그인'), findsOneWidget);
    expect(find.byIcon(Icons.open_in_browser), findsOneWidget);
    expect(find.text('Baron SSO로 로그인'), findsOneWidget);
  });

  testWidgets('does not collect phone number inside the app', (tester) async {
    if (_assumeLoggedIn) {
      return;
    }

    app.main();
    await tester.pumpAndSettle();

    expect(find.byType(TextField), findsNothing);
    expect(find.textContaining('Hosted Login'), findsOneWidget);
  });

  testWidgets('manual hosted login starts from the SSO button', (
    tester,
  ) async {
    if (_assumeLoggedIn) {
      return;
    }

    app.main();
    await tester.pumpAndSettle();

    expect(find.text('Baron SSO로 로그인'), findsOneWidget);
  });

  testWidgets('checks post-login directory actions when session is preseeded', (
    tester,
  ) async {
    if (!_assumeLoggedIn) {
      return;
    }

    app.main();
    await tester.pumpAndSettle();

    expect(find.byType(TextField), findsOneWidget);
    expect(find.textContaining('검색 결과'), findsOneWidget);

    final addFavorite = find.byTooltip('즐겨찾기 추가');
    if (addFavorite.evaluate().isNotEmpty) {
      await tester.tap(addFavorite.first);
      await tester.pumpAndSettle();
      await tester.tap(find.text('즐겨찾기').first);
      await tester.pumpAndSettle();
      expect(find.textContaining('검색 결과'), findsOneWidget);
    }

    final employeeTile = find.byType(ListTile);
    expect(employeeTile, findsWidgets);

    await tester.tap(employeeTile.first);
    await tester.pumpAndSettle();

    expect(find.text('전화'), findsOneWidget);
    expect(find.text('문자'), findsOneWidget);
  });
}
