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/features/tasks/domain/task_sort_option.dart';
import 'package:household_keeper/features/tasks/presentation/sort_preference_notifier.dart';

/// Helper: create a container and wait for the initial async _loadPersisted()
/// to finish.
Future<ProviderContainer> makeContainer() async {
  final container = ProviderContainer();
  // Trigger build
  container.read(sortPreferenceProvider);
  // Allow the async _loadPersisted() to complete
  await Future<void>.delayed(Duration.zero);
  return container;
}

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

  group('SortPreferenceNotifier', () {
    test('build() returns default state of alphabetical', () async {
      final container = ProviderContainer();
      addTearDown(container.dispose);

      final state = container.read(sortPreferenceProvider);

      expect(state, TaskSortOption.alphabetical);
    });

    test('setSortOption(interval) updates state to interval', () async {
      final container = await makeContainer();
      addTearDown(container.dispose);

      await container
          .read(sortPreferenceProvider.notifier)
          .setSortOption(TaskSortOption.interval);

      expect(container.read(sortPreferenceProvider), TaskSortOption.interval);
    });

    test('setSortOption(effort) updates state to effort', () async {
      final container = await makeContainer();
      addTearDown(container.dispose);

      await container
          .read(sortPreferenceProvider.notifier)
          .setSortOption(TaskSortOption.effort);

      expect(container.read(sortPreferenceProvider), TaskSortOption.effort);
    });

    test('setSortOption persists to SharedPreferences', () async {
      final container = await makeContainer();
      addTearDown(container.dispose);

      await container
          .read(sortPreferenceProvider.notifier)
          .setSortOption(TaskSortOption.effort);

      final prefs = await SharedPreferences.getInstance();
      expect(prefs.getString('task_sort_option'), 'effort');
    });

    test('persisted value is loaded on restart (effort)', () async {
      SharedPreferences.setMockInitialValues({
        'task_sort_option': 'effort',
      });

      final container = await makeContainer();
      addTearDown(container.dispose);

      expect(container.read(sortPreferenceProvider), TaskSortOption.effort);
    });

    test('persisted value is loaded on restart (interval)', () async {
      SharedPreferences.setMockInitialValues({
        'task_sort_option': 'interval',
      });

      final container = await makeContainer();
      addTearDown(container.dispose);

      expect(container.read(sortPreferenceProvider), TaskSortOption.interval);
    });

    test('unknown persisted value falls back to alphabetical', () async {
      SharedPreferences.setMockInitialValues({
        'task_sort_option': 'unknown_value',
      });

      final container = await makeContainer();
      addTearDown(container.dispose);

      expect(container.read(sortPreferenceProvider), TaskSortOption.alphabetical);
    });
  });
}
