import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

import 'package:lanebow/screens/club/club_settings_screen.dart';
import 'package:lanebow/models/club_model.dart';
import 'package:lanebow/models/member_model.dart';
import 'package:lanebow/models/user_model.dart';
import '../../utils/mock_services.dart';

void main() {
  Widget wrapWithProviders({required List<Member> members, required ValueChanged<int> onBuilt}) {
    final club = Club(id: 'c1', name: '테스트클럽', ownerId: 'u1');
    final user = User(id: 'u1', email: 'a@a.com', name: 'Admin', role: 'admin');

    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => MockAuthService(user: user)),
        ChangeNotifierProvider(create: (_) => MockClubService(club: club)),
        ChangeNotifierProvider(create: (_) => MockMemberService(seed: members)),
      ],
      child: MaterialApp(
        home: Scaffold(
          body: ClubSettingsScreen(
            onOwnerItemsBuilt: onBuilt,
            initialMembers: members,
          ),
        ),
      ),
    );
  }

  group('ClubSettingsScreen - owner dropdown items', () {
    testWidgets('reports count when members exist', (tester) async {
      final seed = [
        Member(id: 'm1', userId: 'u1', clubId: 'c1', name: 'Alice', email: 'a@a.com', isActive: true, memberType: 'owner'),
        Member(id: 'm2', userId: 'u2', clubId: 'c1', name: 'Bob', email: 'b@b.com', isActive: true, memberType: 'manager'),
      ];
      final completer = Completer<int>();

      await tester.pumpWidget(wrapWithProviders(
        members: seed,
        onBuilt: (c) => completer.complete(c),
      ));
      await tester.pump();

      final count = await completer.future.timeout(const Duration(seconds: 1));
      expect(count, 2);
    });

    testWidgets('reports zero when members list is empty', (tester) async {
      final completer = Completer<int>();

      await tester.pumpWidget(wrapWithProviders(
        members: const [],
        onBuilt: (c) => completer.complete(c),
      ));
      await tester.pump();

      final count = await completer.future.timeout(const Duration(seconds: 1));
      expect(count, 0);
    });
  });
}
