import 'package:equatable/equatable.dart';

import '../../../domain/entities/alarm_state.dart' as domain;

class AlarmBlocState extends Equatable {
  final List<domain.AlarmState> activeAlarms;
  final bool isLoading;
  final String? error;
  final DateTime? lastSync;

  const AlarmBlocState({
    this.activeAlarms = const [],
    this.isLoading = false,
    this.error,
    this.lastSync,
  });

  /// Get count of active blocking alarms
  int get blockingAlarmCount =>
      activeAlarms.where((a) => a.blocksMonitor).length;

  /// Get alarms for a specific monitor
  List<domain.AlarmState> alarmsForMonitor(int monitorId) {
    return activeAlarms
        .where((a) => a.associatedMonitor == monitorId)
        .toList();
  }

  /// Check if monitor has blocking alarm
  bool monitorHasBlockingAlarm(int monitorId) {
    return activeAlarms.any(
        (a) => a.associatedMonitor == monitorId && a.blocksMonitor);
  }

  AlarmBlocState copyWith({
    List<domain.AlarmState>? activeAlarms,
    bool? isLoading,
    String? error,
    DateTime? lastSync,
  }) {
    return AlarmBlocState(
      activeAlarms: activeAlarms ?? this.activeAlarms,
      isLoading: isLoading ?? this.isLoading,
      error: error,
      lastSync: lastSync ?? this.lastSync,
    );
  }

  @override
  List<Object?> get props => [activeAlarms, isLoading, error, lastSync];
}
