import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:household_keeper/core/router/router.dart';
import 'package:household_keeper/features/home/domain/calendar_models.dart';
import 'package:household_keeper/features/home/presentation/calendar_providers.dart';
import 'package:household_keeper/features/rooms/presentation/room_providers.dart';
import 'package:household_keeper/l10n/app_localizations.dart';

void main() {
  group('AppShell Navigation', () {
    setUp(() {
      SharedPreferences.setMockInitialValues({});
    });

    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);

    /// Helper to build the app with providers overridden for testing.
    ///
    /// Uses [UncontrolledProviderScope] with a [ProviderContainer] to avoid
    /// the riverpod_lint scoped_providers_should_specify_dependencies warning.
    Widget buildApp() {
      final container = ProviderContainer(overrides: [
        // Override the stream provider to return an empty list immediately
        // so that the rooms screen shows the empty state without needing a DB.
        roomWithStatsListProvider.overrideWith(
          (ref) => Stream.value([]),
        ),
        // Override selected date to avoid any DB access.
        selectedDateProvider.overrideWith(SelectedDateNotifier.new),
        // Override calendar day provider to return empty first-run state so
        // HomeScreen renders without a database.
        calendarDayProvider.overrideWith(
          (ref) => Stream.value(CalendarDayState(
            selectedDate: today,
            dayTasks: const [],
            overdueTasks: const [],
            totalTaskCount: 0,
          )),
        ),
      ]);

      return UncontrolledProviderScope(
        container: container,
        child: MaterialApp.router(
          routerConfig: router,
          localizationsDelegates: AppLocalizations.localizationsDelegates,
          supportedLocales: const [Locale('de')],
          locale: const Locale('de'),
        ),
      );
    }

    testWidgets('renders 3 navigation destinations with correct German labels',
        (tester) async {
      await tester.pumpWidget(buildApp());
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // Verify 3 NavigationDestination widgets are rendered
      expect(find.byType(NavigationDestination), findsNWidgets(3));

      // Verify correct German labels from ARB (with umlauts).
      // 'Übersicht' appears in both the bottom nav and the HomeScreen AppBar.
      expect(find.text('\u00dcbersicht'), findsWidgets);
      expect(find.text('R\u00e4ume'), findsOneWidget);
      expect(find.text('Einstellungen'), findsOneWidget);
    });

    testWidgets('tapping a destination changes the selected tab',
        (tester) async {
      await tester.pumpWidget(buildApp());
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // Initially on Home tab (index 0) -- verify home first-run empty state
      expect(find.text('Noch keine Aufgaben angelegt'), findsOneWidget);

      // Tap the Rooms tab (second destination)
      await tester.tap(find.text('R\u00e4ume'));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // Verify we see Rooms content now (empty state)
      expect(find.text('Hier ist noch alles leer!'), findsOneWidget);

      // Tap the Settings tab (third destination)
      await tester.tap(find.text('Einstellungen'));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 500));

      // Verify we see Settings content now
      expect(find.text('Darstellung'), findsOneWidget);
    });
  });
}
