class AppEvent {
  const AppEvent({required this.type, required this.payload});

  final String type;
  final Map<String, dynamic> payload;

  factory AppEvent.fromJson(Map<String, dynamic> json) {
    final dynamic rawPayload = json['data'] ?? json['payload'] ?? const <String, dynamic>{};
    return AppEvent(
      type: json['type'] as String? ?? 'unknown',
      payload: _normalizePayload(rawPayload),
    );
  }

  static Map<String, dynamic> _normalizePayload(dynamic payload) {
    if (payload is Map<String, dynamic>) {
      return payload;
    }
    if (payload is Map) {
      return Map<String, dynamic>.from(payload);
    }
    return <String, dynamic>{'value': payload};
  }
}
