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

import 'package:lanebow/screens/club/event_form_screen.dart';
import 'package:lanebow/services/event_service.dart';

import '../../utils/stub_event_service.dart';

void main() {
  group('EventFormScreen access password', () {
    testWidgets('Empty password sends accessPassword: null on create', (tester) async {
      // Arrange
      final stub = StubEventService();

      await stub.initialize('token');
      stub.setClubId('club1');

      await tester.pumpWidget(
        MultiProvider(
          providers: [
            ChangeNotifierProvider<EventService>.value(value: stub),
          ],
          child: const MaterialApp(
            home: Scaffold(body: EventFormScreen()),
          ),
        ),
      );

      await tester.pumpAndSettle();

      // Fill minimal required: title
      final titleField = find.byType(TextFormField).first;
      await tester.enterText(titleField, '테스트 이벤트');
      await tester.pump();

      // access_password_field is empty by default. Call save via testing hook.
      final state = tester.state<State<StatefulWidget>>(find.byType(EventFormScreen)) as dynamic;
      await state.invokeSaveForTest();
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 50));

      // Assert: stub captured payload with accessPassword == null
      expect(stub.lastCreatedEventData, isNotNull);
      expect(stub.lastCreatedEventData!['accessPassword'], isNull);
    });

    testWidgets('Validator blocks <4 chars and toggle visibility works', (tester) async {
      // Arrange
      final stub = StubEventService();

      await stub.initialize('token');
      stub.setClubId('club1');

      await tester.pumpWidget(
        MultiProvider(
          providers: [
            ChangeNotifierProvider<EventService>.value(value: stub),
          ],
          child: const MaterialApp(
            home: Scaffold(body: EventFormScreen()),
          ),
        ),
      );

      await tester.pumpAndSettle();

      // Fill required: title
      final titleField = find.byType(TextFormField).first;
      await tester.enterText(titleField, '테스트 이벤트');
      await tester.pump();

      // Enter 3-char password
      final pwField = find.byKey(const ValueKey('access_password_field'));
      expect(pwField, findsOneWidget);
      await tester.enterText(pwField, 'abc');
      await tester.pump();

      // Try saving via hook -> expect validator message
      final state = tester.state<State<StatefulWidget>>(find.byType(EventFormScreen)) as dynamic;
      await state.invokeSaveForTest();
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 50));

      expect(find.text('비밀번호는 최소 4자 이상이어야 합니다'), findsOneWidget);

      // Toggle visibility via tooltip (ensure on-screen first)
      await tester.ensureVisible(pwField);
      await tester.pump();
      final toggleBtn = find.byTooltip('비밀번호 표시');
      expect(toggleBtn, findsOneWidget);
      await tester.ensureVisible(toggleBtn);
      await tester.pump();
      await tester.tap(toggleBtn);
      await tester.pump();

      // After toggle once, tooltip should switch to '비밀번호 숨김'
      expect(find.byTooltip('비밀번호 숨김'), findsOneWidget);
    });
  });
}
