class RealtimeMonitoringModel {
  final String deviceId;
  final String roomId;
  final bool servoOpen;
  final DateTime lastCommand;
  final String deviceStatus;
  final double dropRate;
  final DateTime lastUpdate;
  final int servoAngleOpen;
  final int servoAngleClose;
  final bool buzzerActive; // ← tambah ini

  RealtimeMonitoringModel({
    required this.deviceId,
    required this.roomId,
    required this.servoOpen,
    required this.lastCommand,
    required this.deviceStatus,
    required this.dropRate,
    required this.lastUpdate,
    this.servoAngleOpen = 90,
    this.servoAngleClose = 0,
    this.buzzerActive = false, // ← tambah ini
  });

  factory RealtimeMonitoringModel.fromRealtimeDB(
    String deviceId,
    String roomId,
    Map<dynamic, dynamic> data,
  ) {
    final controlling = data['controlling'] as Map<dynamic, dynamic>? ?? {};
    final monitoring = data['monitoring'] as Map<dynamic, dynamic>? ?? {};
    final settings = data['settings'] as Map<dynamic, dynamic>? ?? {};

    return RealtimeMonitoringModel(
      deviceId: deviceId,
      roomId: roomId,
      servoOpen: controlling['servo_open'] ?? false,
      buzzerActive: controlling['buzzer_active'] ?? false, // ← tambah ini
      lastCommand: DateTime.parse(
        controlling['last_command'] ?? DateTime.now().toIso8601String(),
      ),
      deviceStatus: monitoring['device_status'] ?? 'disconnected',
      dropRate: (monitoring['drop_rate'] ?? 0).toDouble(),
      lastUpdate: DateTime.parse(
        monitoring['last_update'] ?? DateTime.now().toIso8601String(),
      ),
      servoAngleOpen: settings['servo_angle_open'] ?? 90,
      servoAngleClose: settings['servo_angle_close'] ?? 0,
    );
  }

  Map<String, dynamic> toRealtimeDB() {
    return {
      'controlling': {
        'servo_open': servoOpen,
        'buzzer_active': buzzerActive, // ← tambah ini
        'last_command': lastCommand.toIso8601String(),
      },
      'monitoring': {
        'device_status': deviceStatus,
        'drop_rate': dropRate,
        'last_update': lastUpdate.toIso8601String(),
      },
      'settings': {
        'servo_angle_open': servoAngleOpen,
        'servo_angle_close': servoAngleClose,
      },
    };
  }

  RealtimeMonitoringModel copyWith({
    String? deviceId,
    String? roomId,
    bool? servoOpen,
    bool? buzzerActive, // ← tambah ini
    DateTime? lastCommand,
    String? deviceStatus,
    double? dropRate,
    DateTime? lastUpdate,
    int? servoAngleOpen,
    int? servoAngleClose,
  }) {
    return RealtimeMonitoringModel(
      deviceId: deviceId ?? this.deviceId,
      roomId: roomId ?? this.roomId,
      servoOpen: servoOpen ?? this.servoOpen,
      buzzerActive: buzzerActive ?? this.buzzerActive, // ← tambah ini
      lastCommand: lastCommand ?? this.lastCommand,
      deviceStatus: deviceStatus ?? this.deviceStatus,
      dropRate: dropRate ?? this.dropRate,
      lastUpdate: lastUpdate ?? this.lastUpdate,
      servoAngleOpen: servoAngleOpen ?? this.servoAngleOpen,
      servoAngleClose: servoAngleClose ?? this.servoAngleClose,
    );
  }
}