import 'global_key_utils.dart';
import '../media/ids.dart';

/// Mixin providing hierarchical event matching methods.
///
/// Events that represent changes to media items often need to check if they
/// affect a specific item or any of its parents in the hierarchy. This mixin
/// provides common matching logic for such events.
mixin HierarchicalEventMixin {
  /// The id of the affected item (Plex ratingKey, Jellyfin GUID, …).
  String get itemId;

  String get globalKey;

  ServerId get serverId;

  /// Parent chain for hierarchical matching.
  /// For an episode: [seasonId, showId]
  /// For a season: [showId]
  /// For a movie: []
  List<String> get parentChain;

  /// Check if this event affects a specific item by id.
  bool affectsItem(String itemId) => this.itemId == itemId || parentChain.contains(itemId);

  /// Check if this event affects any item in a collection.
  bool affectsAnyOf(Iterable<String> itemIds) {
    if (itemIds.contains(itemId)) return true;
    return parentChain.any(itemIds.contains);
  }

  /// Check if this event affects any item in a global-key collection.
  bool affectsAnyGlobalKey(Iterable<String> globalKeys) {
    if (globalKeys.contains(globalKey)) return true;
    for (final parentId in parentChain) {
      if (globalKeys.contains(buildGlobalKey(serverId, parentId))) return true;
    }
    return false;
  }
}
