import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:werwolf/screens/playerregistry.dart';
import 'package:werwolf/screens/settings.dart';

void main() {
  group('PlayerRegistry Tests', () {
    testWidgets('Adds a player to the list', (WidgetTester tester) async {
      await tester.pumpWidget(const MaterialApp(home: PlayerRegistry()));

      await tester.enterText(find.byType(TextField), 'Alice');
      await tester.testTextInput.receiveAction(TextInputAction.done);
      await tester.pumpAndSettle();

      expect(find.text('Alice'), findsOneWidget);
    });

    testWidgets('Shows error for duplicate player', (WidgetTester tester) async {
      await tester.pumpWidget(const MaterialApp(home: PlayerRegistry()));

      await tester.enterText(find.byType(TextField), 'Alice');
      await tester.testTextInput.receiveAction(TextInputAction.done);
      await tester.pumpAndSettle();

      await tester.enterText(find.byType(TextField), 'Alice');
      await tester.testTextInput.receiveAction(TextInputAction.done);
      await tester.pumpAndSettle();

      expect(find.text('Dieser Spieler existiert bereits'), findsOneWidget);
    });

    testWidgets('Navigates to GameSettings when enough players are added', (WidgetTester tester) async {
      await tester.pumpWidget(const MaterialApp(home: PlayerRegistry()));

      for (int i = 0; i < 6; i++) {
        await tester.enterText(find.byType(TextField), 'Player $i');
        await tester.testTextInput.receiveAction(TextInputAction.done);
        await tester.pumpAndSettle();
      }

      await tester.tap(find.text('Spiel einstellen'));
      await tester.pumpAndSettle();

      expect(find.byType(GameSettings), findsOneWidget);
    });
  });
}
