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/score_model.dart';
import 'package:lanebow/models/team_model.dart';
import 'package:lanebow/screens/club/tabs/teams_tab.dart';

void main() {
  testWidgets('미배정 참가자 Chip onDeleted → onShowTeamSelectionDialog 호출', (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: []),
    ];
    final unassigned = <Participant>[
      Participant(id: 'p3', eventId: 'ev1', memberId: 'm3', name: 'C'),
    ];

    Participant? tapped;

    await tester.pumpWidget(
      MaterialApp(
        home: Scaffold(
          body: TeamsTab(
            event: event,
            isLoading: false,
            teams: teams,
            participants: participants + unassigned,
            scores: const <Score>[],
            teamGenerationMethod: 1,
            teamSizeController: TextEditingController(text: '4'),
            teamCountController: TextEditingController(text: '2'),
            unassignedParticipants: unassigned,
            onChangeTeamGenerationMethod: (_) {},
            onGenerateTeams: () {},
            onSaveTeams: () {},
            onRemoveFromTeam: (_, __) {},
            onShowParticipantSelectionDialog: (_) {},
            onShowTeamSelectionDialog: (p) { tapped = p; },
            calculateTeamAverage: (_) => 0,
            onShareEvent: () {},
            onMoveParticipant: null,
          ),
        ),
      ),
    );
    await tester.pumpAndSettle();

    // when: 미배정 Chip의 delete 아이콘을 탭
    final chipFinder = find.byType(Chip);
    expect(chipFinder, findsWidgets);

    // 첫 번째 Chip의 delete 아이콘 탭 (onDeleted 실행)
    await tester.tap(find.descendant(of: chipFinder.first, matching: find.byIcon(Icons.add_circle)));
    await tester.pump();

    // then
    expect(tapped, isNotNull);
    expect(tapped!.id, 'p3');
  });
}
