import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:agenda_tasks/features/tasks/presentation/viewmodels/daily_tasks_viewmodel.dart';
import 'package:agenda_tasks/features/tasks/presentation/widgets/filter_chips.dart';

import '../../../../helpers/test_helpers.dart';

void main() {
  group('FilterChips', () {
    late TaskFilter selectedFilter;

    Widget buildFilterChips({TaskFilter initialFilter = TaskFilter.all}) {
      selectedFilter = initialFilter;
      return createTestableWidget(
        StatefulBuilder(
          builder: (context, setState) {
            return FilterChips(
              currentFilter: selectedFilter,
              onFilterChanged: (filter) {
                setState(() {
                  selectedFilter = filter;
                });
              },
            );
          },
        ),
      );
    }

    testWidgets('should display all three filter options', (tester) async {
      await tester.pumpWidget(buildFilterChips());
      await tester.pumpAndSettle();

      expect(find.text('All'), findsOneWidget);
      expect(find.text('Active'), findsOneWidget);
      expect(find.text('Completed'), findsOneWidget);
    });

    testWidgets('should have "All" chip selected by default', (tester) async {
      await tester.pumpWidget(buildFilterChips());
      await tester.pumpAndSettle();

      final chips = tester.widgetList<FilterChip>(find.byType(FilterChip));
      final allChip = chips.first;

      expect(allChip.selected, true);
    });

    testWidgets('should select "Active" chip when tapped', (tester) async {
      await tester.pumpWidget(buildFilterChips());
      await tester.pumpAndSettle();

      await tester.tap(find.text('Active'));
      await tester.pumpAndSettle();

      expect(selectedFilter, TaskFilter.active);
    });

    testWidgets('should select "Completed" chip when tapped', (tester) async {
      await tester.pumpWidget(buildFilterChips());
      await tester.pumpAndSettle();

      await tester.tap(find.text('Completed'));
      await tester.pumpAndSettle();

      expect(selectedFilter, TaskFilter.completed);
    });

    testWidgets('should select "All" chip when tapped from another filter', (tester) async {
      await tester.pumpWidget(buildFilterChips(initialFilter: TaskFilter.active));
      await tester.pumpAndSettle();

      await tester.tap(find.text('All'));
      await tester.pumpAndSettle();

      expect(selectedFilter, TaskFilter.all);
    });

    testWidgets('should visually indicate selected filter', (tester) async {
      await tester.pumpWidget(buildFilterChips(initialFilter: TaskFilter.completed));
      await tester.pumpAndSettle();

      final chips = tester.widgetList<FilterChip>(find.byType(FilterChip)).toList();

      // All chip should not be selected
      expect(chips[0].selected, false);
      // Active chip should not be selected
      expect(chips[1].selected, false);
      // Completed chip should be selected
      expect(chips[2].selected, true);
    });

    testWidgets('should be horizontally scrollable', (tester) async {
      await tester.pumpWidget(buildFilterChips());
      await tester.pumpAndSettle();

      expect(find.byType(SingleChildScrollView), findsOneWidget);
    });
  });
}
