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 StubScoreService extends PublicEventService {
  String? lastHash;
  String? lastParticipantId;
  int? lastGameNumber;
  int? lastScore;

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

  @override
  Future<void> updateScore(String publicHash, {required String participantId, required int gameNumber, required int score, int? handicap, dynamic teamNumber}) async {
    lastHash = publicHash;
    lastParticipantId = participantId;
    lastGameNumber = gameNumber;
    lastScore = score;
    await Future<void>.delayed(const Duration(milliseconds: 10));
  }
}

void main() {
  testWidgets('tap participant -> edit game1 score -> calls updateScore and shows snackbar', (tester) async {
    final stub = StubScoreService();
    await tester.pumpWidget(MaterialApp(
      home: PublicEventScreen(publicHash: 'hash123', service: stub),
    ));

    await tester.pumpAndSettle();
    expect(find.byKey(const Key('participant_tile_p1')), findsOneWidget);

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

    // 입력 후 저장
    await tester.enterText(find.byKey(const Key('score_input')), '199');
    await tester.tap(find.byKey(const Key('score_save_button')));
    await tester.pumpAndSettle();

    // 스낵바 문구 확인
    expect(find.text('점수가 저장되었습니다'), findsOneWidget);

    // 서비스 호출 확인
    expect(stub.lastHash, 'hash123');
    expect(stub.lastParticipantId, 'p1');
    expect(stub.lastGameNumber, 1);
    expect(stub.lastScore, 199);
  });
}
