import 'dart:convert';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:moonwell_launcher/features/launcher/domain/entities/launcher_session.dart';
import 'package:moonwell_launcher/features/preferences/data/repositories/local_preferences_repository.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  group('LocalPreferencesRepository', () {
    setUp(() {
      SharedPreferences.setMockInitialValues(<String, Object>{});
      FlutterSecureStorage.setMockInitialValues(<String, String>{});
    });

    Future<LocalPreferencesRepository> createRepository() async {
      final preferences = await SharedPreferences.getInstance();
      return LocalPreferencesRepository(
        preferences: preferences,
        secureStorage: const FlutterSecureStorage(),
      );
    }

    test('persists output directory through shared preferences', () async {
      final repository = await createRepository();
      final uri = Uri.file(r'C:\Games\MoonWell');

      await repository.setOutputDir(uri);

      expect(await repository.getOutputDir(), uri);
    });

    test('persists launcher session through secure storage', () async {
      final repository = await createRepository();
      final session = LauncherSession(
        accessToken: 'token',
        tokenType: 'Bearer',
        expiresAt: DateTime.parse('2030-01-01T00:00:00Z'),
      );

      await repository.setLauncherSession(session);
      final restoredSession = await repository.getLauncherSession();

      expect(restoredSession?.accessToken, 'token');
      expect(restoredSession?.tokenType, 'Bearer');
      expect(
        restoredSession?.expiresAt?.toUtc(),
        DateTime.parse('2030-01-01T00:00:00Z'),
      );
    });

    test('clears persisted launcher session', () async {
      final repository = await createRepository();

      await repository.setLauncherSession(
        const LauncherSession(
          accessToken: 'token',
          tokenType: 'Bearer',
          expiresAt: null,
        ),
      );
      await repository.clearLauncherSession();

      expect(await repository.getLauncherSession(), isNull);
    });

    test(
      'removes legacy shared preferences session without restoring it',
      () async {
        final legacySession = LauncherSession(
          accessToken: 'legacy-token',
          tokenType: 'Bearer',
          expiresAt: DateTime.parse('2030-01-01T00:00:00Z'),
        );
        SharedPreferences.setMockInitialValues(<String, Object>{
          'launcher_session': jsonEncode(legacySession.toJson()),
        });
        final preferences = await SharedPreferences.getInstance();
        final repository = LocalPreferencesRepository(
          preferences: preferences,
          secureStorage: const FlutterSecureStorage(),
        );

        expect(await repository.getLauncherSession(), isNull);
        expect(preferences.containsKey('launcher_session'), isFalse);
      },
    );
  });
}
