import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/event_model.dart';
import 'package:lanebow/models/participant_model.dart';
import 'package:lanebow/models/team_model.dart';
import 'package:lanebow/screens/club/event_details_screen.dart';

void main() {
  setUp(() {
    EventDetailsScreen.testMode = true;
  });
  tearDown(() {
    EventDetailsScreen.testMode = false;
  });

  group('_moveParticipantBetweenTeams 동작 테스트', () {
    testWidgets('A팀의 m1을 B팀 끝으로 이동하면 멤버 배열이 반영된다', (tester) async {
      // given
      final event = Event(
        id: 'ev1', clubId: 'c1', title: 'E', type: 'team', status: 'draft', startDate: DateTime.now(), isActive: true,
      );
      final participants = <Participant>[
        Participant(id: 'p1', eventId: 'ev1', memberId: 'm1', name: 'A'),
        Participant(id: 'p2', eventId: 'ev1', memberId: 'm2', name: 'B'),
      ];
      final teams = <Team>[
        Team(id: 't1', eventId: 'ev1', name: 'A', memberIds: ['m1']),
        Team(id: 't2', eventId: 'ev1', name: 'B', memberIds: ['m2']),
      ];

      await tester.pumpWidget(MaterialApp(home: EventDetailsScreen(event: event)));
      await tester.pumpAndSettle();

      final state = tester.state(find.byType(EventDetailsScreen));
      final dynamic dyn = state;

      // 초기 주입
      dyn.debugSetTeamsAndParticipants(teams, participants);
      await tester.pump();

      // when: 이동 실행 (toIndex = 대상팀 끝)
      dyn.testMoveParticipantBetweenTeams(
        participants.firstWhere((e) => e.memberId == 'm1'),
        teams.first,
        teams.last,
        teams.last.memberIds.length,
      );
      await tester.pump();

      // then
      final after = (dyn.debugGetTeams() as List<Team>);
      expect(after[0].memberIds, isEmpty);
      expect(after[1].memberIds, ['m2', 'm1']);
    });
  });
}
