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/notifications/notification_settings_notifier.dart';
import 'package:household_keeper/core/theme/theme_provider.dart';
import 'package:household_keeper/features/settings/presentation/settings_screen.dart';
import 'package:household_keeper/l10n/app_localizations.dart';

/// Build a standalone SettingsScreen with provider overrides for test isolation.
Widget _buildSettings(NotificationSettings notifSettings) {
  final container = ProviderContainer(overrides: [
    notificationSettingsProvider.overrideWithValue(notifSettings),
    themeProvider.overrideWithValue(ThemeMode.system),
  ]);

  return UncontrolledProviderScope(
    container: container,
    child: const MaterialApp(
      localizationsDelegates: AppLocalizations.localizationsDelegates,
      supportedLocales: [Locale('de')],
      locale: Locale('de'),
      home: Scaffold(body: SettingsScreen()),
    ),
  );
}

void main() {
  setUp(() {
    SharedPreferences.setMockInitialValues({});
  });

  group('SettingsScreen Benachrichtigungen section', () {
    testWidgets('renders Benachrichtigungen section header', (tester) async {
      await tester.pumpWidget(_buildSettings(const NotificationSettings(
        enabled: false,
        time: TimeOfDay(hour: 7, minute: 0),
      )));
      await tester.pumpAndSettle();

      expect(find.text('Benachrichtigungen'), findsOneWidget);
    });

    testWidgets(
      'notification toggle displays with correct label and defaults to OFF',
      (tester) async {
        await tester.pumpWidget(_buildSettings(const NotificationSettings(
          enabled: false,
          time: TimeOfDay(hour: 7, minute: 0),
        )));
        await tester.pumpAndSettle();

        // Label matches ARB notificationsEnabledLabel
        expect(find.text('Tägliche Erinnerung'), findsOneWidget);

        // SwitchListTile with value=false
        final switchTile = tester.widget<SwitchListTile>(
          find.byType(SwitchListTile),
        );
        expect(switchTile.value, isFalse);
      },
    );

    testWidgets(
      'time picker row is visible when notifications are enabled',
      (tester) async {
        await tester.pumpWidget(_buildSettings(const NotificationSettings(
          enabled: true,
          time: TimeOfDay(hour: 9, minute: 30),
        )));
        await tester.pumpAndSettle();

        // notificationsTimeLabel should be visible
        expect(find.text('Uhrzeit'), findsOneWidget);
        // Formatted time should be shown (09:30)
        expect(find.text('09:30'), findsOneWidget);
      },
    );

    testWidgets(
      'time picker row is hidden when notifications are disabled',
      (tester) async {
        await tester.pumpWidget(_buildSettings(const NotificationSettings(
          enabled: false,
          time: TimeOfDay(hour: 9, minute: 30),
        )));
        await tester.pumpAndSettle();

        // notificationsTimeLabel should NOT be visible
        expect(find.text('Uhrzeit'), findsNothing);
      },
    );

    testWidgets(
      'time picker row displays correctly formatted time when enabled',
      (tester) async {
        await tester.pumpWidget(_buildSettings(const NotificationSettings(
          enabled: true,
          time: TimeOfDay(hour: 7, minute: 0),
        )));
        await tester.pumpAndSettle();

        // Default time 07:00 should display as formatted string
        expect(find.text('07:00'), findsOneWidget);
      },
    );
  });
}
