import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:agenda_tasks/app.dart';
import 'package:agenda_tasks/core/di/injection_container.dart' as di;
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  group('End-to-End App Flow', () {
    setUpAll(() async {
      // Set up mock SharedPreferences to bypass setup/onboarding
      SharedPreferences.setMockInitialValues({
        'setup_completed': true,
        'onboarding_shown': true,
        'app_mode': 'local',
        'locale': 'en',
        'theme_mode': 'system',
      });

      // Initialize DI
      await di.init();
    });

    testWidgets('Task creation flow: navigate, create task, verify display',
        (tester) async {
      // Launch the app
      await tester.pumpWidget(const AgendaApp());
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Verify we're on the Daily Agenda page
      expect(find.text('Agenda Tasks'), findsOneWidget);

      // Verify FAB is present
      final fab = find.byType(FloatingActionButton);
      expect(fab, findsOneWidget);

      // Tap FAB to create new task
      await tester.tap(fab);
      await tester.pumpAndSettle();

      // Verify we're on task form page
      expect(find.text('New Task'), findsOneWidget);

      // Find title field and enter text
      final titleField = find.widgetWithText(TextFormField, 'Title');
      expect(titleField, findsOneWidget);
      await tester.enterText(titleField, 'E2E Test Task');
      await tester.pumpAndSettle();

      // Tap save button
      final saveButton = find.text('Save');
      expect(saveButton, findsOneWidget);
      await tester.tap(saveButton);
      await tester.pumpAndSettle(const Duration(seconds: 1));

      // Verify we're back on daily agenda and task is displayed
      expect(find.text('Agenda Tasks'), findsOneWidget);
      expect(find.text('E2E Test Task'), findsOneWidget);
    });

    testWidgets('Task toggle flow: mark task as completed', (tester) async {
      await tester.pumpWidget(const AgendaApp());
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Check if there's a task to toggle
      final checkboxes = find.byType(Checkbox);
      if (checkboxes.evaluate().isNotEmpty) {
        // Get initial state
        final checkbox = tester.widget<Checkbox>(checkboxes.first);
        final initialValue = checkbox.value ?? false;

        // Tap to toggle
        await tester.tap(checkboxes.first);
        await tester.pumpAndSettle();

        // Verify state changed
        final updatedCheckbox = tester.widget<Checkbox>(checkboxes.first);
        expect(updatedCheckbox.value, equals(!initialValue));
      }
    });

    testWidgets('Filter functionality: switch between All, Active, Completed',
        (tester) async {
      await tester.pumpWidget(const AgendaApp());
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Verify filter chips are present
      expect(find.text('All'), findsOneWidget);
      expect(find.text('Active'), findsOneWidget);
      expect(find.text('Completed'), findsOneWidget);

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

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

      // Tap All filter to reset
      await tester.tap(find.text('All'));
      await tester.pumpAndSettle();

      // App should still be functional
      expect(find.text('Agenda Tasks'), findsOneWidget);
    });

    testWidgets('Navigation: Calendar and Settings access', (tester) async {
      await tester.pumpWidget(const AgendaApp());
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Navigate to Calendar
      final calendarButton = find.byIcon(Icons.calendar_month);
      expect(calendarButton, findsOneWidget);
      await tester.tap(calendarButton);
      await tester.pumpAndSettle();

      // Verify Calendar page loaded
      expect(find.text('Calendar'), findsOneWidget);

      // Go back
      await tester.tap(find.byIcon(Icons.arrow_back));
      await tester.pumpAndSettle();

      // Navigate to Settings
      final settingsButton = find.byIcon(Icons.settings);
      expect(settingsButton, findsOneWidget);
      await tester.tap(settingsButton);
      await tester.pumpAndSettle();

      // Verify Settings page loaded
      expect(find.text('Settings'), findsOneWidget);
      expect(find.text('Language'), findsOneWidget);
      expect(find.text('Theme'), findsOneWidget);

      // Go back to main screen
      await tester.tap(find.byIcon(Icons.arrow_back));
      await tester.pumpAndSettle();

      expect(find.text('Agenda Tasks'), findsOneWidget);
    });

    testWidgets('Internationalization: Language elements are displayed',
        (tester) async {
      await tester.pumpWidget(const AgendaApp());
      await tester.pumpAndSettle(const Duration(seconds: 2));

      // Verify localized strings are present (English)
      expect(find.text('Agenda Tasks'), findsOneWidget);
      expect(find.text('All'), findsOneWidget);
      expect(find.text('Active'), findsOneWidget);
      expect(find.text('Completed'), findsOneWidget);
    });
  });
}
