import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'shared/settings_persistence.dart';


class LastPlayedVersionStore with SettingsPersistence {
  static const _key = 'smplayer.last_played_version';
  static const _maxRemembered = 500;

  String _entryKey(String serverId, String itemId) => '$serverId:$itemId';

  Future<void> save({
    required String serverId,
    required String itemId,
    required String mediaSourceId,
  }) async {
    if (serverId.isEmpty || itemId.isEmpty || mediaSourceId.isEmpty) return;
    await putRemembered(_key, _entryKey(serverId, itemId), {
      'mediaSourceId': mediaSourceId,
      'updatedAt': DateTime.now().toIso8601String(),
    }, _maxRemembered);
  }

  Future<String?> get({
    required String serverId,
    required String itemId,
  }) async {
    if (serverId.isEmpty || itemId.isEmpty) return null;
    final entry = await readRemembered(_key, _entryKey(serverId, itemId));
    return entry?['mediaSourceId'] as String?;
  }
}

final lastPlayedVersionStoreProvider = Provider<LastPlayedVersionStore>(
  (ref) => LastPlayedVersionStore(),
);
