import 'package:shared_preferences/shared_preferences.dart';

/// Registo local (por criança) de episódios assistidos até ao fim.
/// Segue o mesmo padrão de [QuizPrefs]: chaves com sufixo `_scopeId`,
/// guardadas via [SharedPreferences], sem qualquer chamada ao Supabase.
class WatchedVideosPrefs {
  static const String _kWatchedKey = 'watched_videos';

  static String _key(String scopeId) => '${_kWatchedKey}_$scopeId';

  static Future<void> markWatched(String scopeId, int videoId) async {
    final prefs = await SharedPreferences.getInstance();
    final key = _key(scopeId);
    final ids = (prefs.getStringList(key) ?? <String>[]).toSet();
    ids.add(videoId.toString());
    await prefs.setStringList(key, ids.toList());
  }

  static Future<bool> isWatched(String scopeId, int videoId) async {
    final prefs = await SharedPreferences.getInstance();
    final ids = prefs.getStringList(_key(scopeId)) ?? const [];
    return ids.contains(videoId.toString());
  }

  static Future<int> getWatchedCount(String scopeId) async {
    final prefs = await SharedPreferences.getInstance();
    final ids = prefs.getStringList(_key(scopeId)) ?? const [];
    return ids.length;
  }
}
