import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/screens/public_event/public_event_screen.dart';
import 'package:lanebow/services/public_event_service.dart';

class StubStateService extends PublicEventService {
  final String status;
  StubStateService(this.status);

  @override
  Future<Map<String, dynamic>> fetchEvent(String publicHash) async {
    return {
      'event': {'id': 'E1', 'title': '상태 테스트', 'gameCount': 3, 'status': status},
      'participants': [
        {
          'participantId': 'p1',
          'name': 'Kim',
          'scores': [null, null, null],
          'handicap': 0,
        },
      ],
      'teams': [],
    };
  }
}

void main() {
  testWidgets('ready: shows join button, blocks score dialog', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: PublicEventScreen(publicHash: 'H', service: StubStateService('ready')),
    ));
    await tester.pumpAndSettle();

    expect(find.byKey(const Key('join_button')), findsOneWidget);

    await tester.tap(find.byKey(const Key('participant_tile_p1')));
    await tester.pumpAndSettle();

    // dialog should NOT appear, snackbar appears
    expect(find.byType(AlertDialog), findsNothing);
    expect(find.textContaining('불가'), findsOneWidget);
  });

  testWidgets('active: shows join button, allows score dialog', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: PublicEventScreen(publicHash: 'H', service: StubStateService('active')),
    ));
    await tester.pumpAndSettle();

    expect(find.byKey(const Key('join_button')), findsOneWidget);

    await tester.tap(find.byKey(const Key('participant_tile_p1')));
    await tester.pumpAndSettle();

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

  testWidgets('completed: hides join button, blocks score dialog', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: PublicEventScreen(publicHash: 'H', service: StubStateService('completed')),
    ));
    await tester.pumpAndSettle();

    expect(find.byKey(const Key('join_button')), findsNothing);

    await tester.tap(find.byKey(const Key('participant_tile_p1')));
    await tester.pumpAndSettle();

    expect(find.byType(AlertDialog), findsNothing);
    expect(find.textContaining('불가'), findsOneWidget);
  });
}
