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 StubTotalsService extends PublicEventService {
  @override
  Future<Map<String, dynamic>> fetchEvent(String publicHash) async {
    return {
      'event': {'id': 'E1', 'title': '합계/평균 테스트', 'gameCount': 3},
      'participants': [
        {
          'participantId': 'p1',
          'name': 'Kim',
          'handicap': 10,
          'scores': [100, 120, 130], // per-game hc 없음 → 기본 hc=10 적용
        },
        {
          'participantId': 'p2',
          'name': 'Lee',
          'handicap': 0,
          'scores': [200, null, 50], // null은 미집계
        }
      ],
      'teams': [
        {
          'teamId': 'T1',
          'teamNumber': 1,
          'handicap': 0,
          'members': [
            {
              'participantId': 'm1',
              'name': 'A',
              'scores': [100, 100, 100]
            },
            {
              'participantId': 'm2',
              'name': 'B',
              'scores': [50, null, 50]
            },
          ]
        }
      ],
    };
  }
}

void main() {
  testWidgets('renders participant totals and team total/avg', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: PublicEventScreen(publicHash: 'hash-totals', service: StubTotalsService()),
    ));

    await tester.pumpAndSettle();

    // 참가자 합계 검증
    // Kim: (100+10) + (120+10) + (130+10) = 380 → 300 cap 없음 → 380
    expect(find.textContaining('총 380'), findsOneWidget);
    // Lee: (200+0) + (50+0) = 250
    expect(find.textContaining('총 250'), findsOneWidget);

    // 팀 합계/평균 검증
    // team members total: A:300, B:100 → 합 400, 팀 hc 0 → 총 400, 인원2 → 평균 200.0
    expect(find.textContaining('총 400'), findsOneWidget);
    expect(find.textContaining('평균 200.0'), findsOneWidget);
  });
}
