import 'package:flutter_test/flutter_test.dart';

import 'package:tdc114plus/src/features/favorites/data/favorites_repository.dart';

void main() {
  test('loads empty favorites when storage is empty', () async {
    final repository = SharedPreferencesFavoritesRepository.withStore(
      _MemoryFavoritesStore(),
    );

    final favorites = await repository.loadFavorites();

    expect(favorites, isEmpty);
  });

  test('toggles favorites in local storage', () async {
    final repository = SharedPreferencesFavoritesRepository.withStore(
      _MemoryFavoritesStore(),
    );

    await repository.toggleFavorite('user-001');
    final added = await repository.loadFavorites();

    expect(added.map((favorite) => favorite.employeeId), ['user-001']);

    await repository.toggleFavorite('user-001');
    final removed = await repository.loadFavorites();

    expect(removed, isEmpty);
  });
}

class _MemoryFavoritesStore implements FavoritesKeyValueStore {
  final _values = <String, String>{};

  @override
  Future<String?> getString(String key) async {
    return _values[key];
  }

  @override
  Future<void> setString(String key, String value) async {
    _values[key] = value;
  }
}
