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

import 'package:recipe_flutter/features/import/data/flyer_import_session.dart';
import 'package:recipe_flutter/features/import/domain/flyer_import_item.dart';
import 'package:recipe_flutter/features/import/domain/flyer_import_result.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  group('FlyerImportSessionNotifier', () {
    setUp(() async {
      SharedPreferences.setMockInitialValues({});
    });

    test('persists only lightweight session payload', () async {
      final container = ProviderContainer();
      addTearDown(container.dispose);

      final notifier = container.read(flyerImportSessionProvider.notifier);
      notifier.setImportedResult(
        result: FlyerImportResult(
          sessionId: 12,
          items: [
            FlyerImportItem(
              flyerItemId: 1,
              rawName: 'Tomat',
              normalizedName: 'tomat',
              isOffer: true,
            ),
          ],
          warnings: const [],
          sourceAvailable: false,
        ),
        selected: const {0: true},
        fileName: 'flyer.pdf',
      );

      await Future<void>.delayed(Duration.zero);

      final prefs = await SharedPreferences.getInstance();
      final raw = prefs.getString('flyer_import_session_v1');
      expect(raw, isNotNull);
      expect(raw!, isNot(contains('rawName')));
      expect(raw, isNot(contains('normalizedName')));
      expect(raw, contains('"sessionId":12'));
    });

    test('restore reads sessionId and selection from storage', () async {
      SharedPreferences.setMockInitialValues({
        'flyer_import_session_v1': '{"sessionId":77,"fileName":"flyer.pdf","selected":{"0":true,"2":false}}',
      });

      final container = ProviderContainer();
      addTearDown(container.dispose);

      final notifier = container.read(flyerImportSessionProvider.notifier);
      await notifier.restore();

      final restored = container.read(flyerImportSessionProvider);
      expect(restored, isNotNull);
      expect(restored!.sessionId, 77);
      expect(restored.fileName, 'flyer.pdf');
      expect(restored.selected[0], true);
      expect(restored.selected[2], false);
      expect(restored.result, isNull);
    });
  });
}
