import '../../../domain/entities/sequence.dart';

class SequenceState {
  final List<SequenceDefinition> sequences;
  final List<SequenceCategory> categories;
  final Map<int, RunningSequence> running; // viewerId -> RunningSequence
  final int? selectedCategoryId;
  final bool isLoading;
  final String? error;

  const SequenceState({
    this.sequences = const [],
    this.categories = const [],
    this.running = const {},
    this.selectedCategoryId,
    this.isLoading = false,
    this.error,
  });

  /// Sequences filtered by selected category
  List<SequenceDefinition> get filteredSequences {
    if (selectedCategoryId == null) return sequences;
    return sequences
        .where((s) => s.categoryId == selectedCategoryId)
        .toList();
  }

  /// Check if a sequence is running on a viewer
  bool isRunningOnViewer(int viewerId) => running.containsKey(viewerId);

  /// Get running sequence for a viewer
  RunningSequence? getRunning(int viewerId) => running[viewerId];

  SequenceState copyWith({
    List<SequenceDefinition>? sequences,
    List<SequenceCategory>? categories,
    Map<int, RunningSequence>? running,
    int? selectedCategoryId,
    bool clearCategory = false,
    bool? isLoading,
    String? error,
    bool clearError = false,
  }) {
    return SequenceState(
      sequences: sequences ?? this.sequences,
      categories: categories ?? this.categories,
      running: running ?? this.running,
      selectedCategoryId: clearCategory
          ? null
          : (selectedCategoryId ?? this.selectedCategoryId),
      isLoading: isLoading ?? this.isLoading,
      error: clearError ? null : (error ?? this.error),
    );
  }
}
