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

import 'package:lanebow/models/club_model.dart';
import 'package:lanebow/models/user_model.dart';
import 'package:lanebow/models/event_model.dart';
import 'package:lanebow/screens/club/events_screen.dart';
import 'package:lanebow/services/auth_service.dart';
import 'package:lanebow/services/club_service.dart';
import 'package:lanebow/services/event_service.dart';

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

void main() {
  group('EventsScreen ellipsis regression', () {
    testWidgets('Event title Text has maxLines=1 and overflow=ellipsis', (tester) async {
      // Arrange providers
      final auth = MockAuthService(
        user: User(
          id: 'u1',
          name: 'Tester',
          email: 'tester@example.com',
          role: 'owner',
        ),
        tokenValue: 'token',
      );
      final club = MockClubService(
        club: Club(
          id: 'club1',
          name: '클럽',
          description: null,
          logo: null,
          address: null,
          location: null,
          phone: null,
          email: null,
          website: null,
          memberCount: 1,
          ownerId: 'u1',
          femaleHandicap: 0,
          averageCalculationPeriod: '3',
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          tieBreakerOptions: const [],
        ),
      );
      final eventsStub = StubEventService();
      await eventsStub.initialize('token');
      eventsStub.setClubId('club1');
      eventsStub.seededEvents = [
        Event(
          id: 'e1',
          clubId: 'club1',
          title: '아주아주아주아주아주아주 긴 이벤트 타이틀입니다 길게 써서 ellipsis가 필요한지 확인합니다',
          startDate: DateTime.now().add(const Duration(days: 1)),
          status: '활성',
          description: null,
          location: '서울특별시 강남구 테헤란로 123 Some Very Very Long Building Name and Floor 12',
          isActive: true,
        ),
      ];

      await tester.pumpWidget(
        MultiProvider(
          providers: [
            ChangeNotifierProvider<AuthService>.value(value: auth),
            ChangeNotifierProvider<ClubService>.value(value: club),
            ChangeNotifierProvider<EventService>.value(value: eventsStub),
          ],
          child: const MaterialApp(home: EventsScreen()),
        ),
      );

      await tester.pumpAndSettle();

      // Act: find the title Text by exact string
      final titleFinder = find.textContaining('아주아주아주아주아주아주 긴 이벤트 타이틀');
      expect(titleFinder, findsOneWidget);
      final Text titleText = tester.widget<Text>(titleFinder);

      // Assert: maxLines and overflow
      expect(titleText.maxLines, 1);
      expect(titleText.overflow, TextOverflow.ellipsis);
    });

    testWidgets('Event location Text has maxLines=1 and overflow=ellipsis', (tester) async {
      // Arrange providers
      final auth = MockAuthService(
        user: User(
          id: 'u1',
          name: 'Tester',
          email: 'tester@example.com',
          role: 'owner',
        ),
        tokenValue: 'token',
      );
      final club = MockClubService(
        club: Club(
          id: 'club1',
          name: '클럽',
          description: null,
          logo: null,
          address: null,
          location: null,
          phone: null,
          email: null,
          website: null,
          memberCount: 1,
          ownerId: 'u1',
          femaleHandicap: 0,
          averageCalculationPeriod: '3',
          createdAt: DateTime.now(),
          updatedAt: DateTime.now(),
          tieBreakerOptions: const [],
        ),
      );
      final eventsStub = StubEventService();
      await eventsStub.initialize('token');
      eventsStub.setClubId('club1');
      eventsStub.seededEvents = [
        Event(
          id: 'e2',
          clubId: 'club1',
          title: '짧은 제목',
          startDate: DateTime.now().add(const Duration(days: 1)),
          status: '활성',
          description: null,
          location: '서울특별시 강남구 테헤란로 123 Some Very Very Long Building Name and Floor 12',
          isActive: true,
        ),
      ];

      await tester.pumpWidget(
        MultiProvider(
          providers: [
            ChangeNotifierProvider<AuthService>.value(value: auth),
            ChangeNotifierProvider<ClubService>.value(value: club),
            ChangeNotifierProvider<EventService>.value(value: eventsStub),
          ],
          child: const MaterialApp(home: EventsScreen()),
        ),
      );

      await tester.pumpAndSettle();

      // Find the location text by a distinctive substring
      final locFinder = find.textContaining('Some Very Very Long Building Name');
      expect(locFinder, findsOneWidget);
      final Text locText = tester.widget<Text>(locFinder);

      expect(locText.maxLines, 1);
      expect(locText.overflow, TextOverflow.ellipsis);
    });
  });
}
