import 'dart:convert';

import 'package:freezed_annotation/freezed_annotation.dart';

part 'signal_message.freezed.dart';
part 'signal_message.g.dart';

/// 信令消息模型，对应 Android 端的 SignalMessage。
///
/// 字段含义：
/// - [type]        消息类型：REGISTER / OFFER / ANSWER / ICE_CANDIDATE
/// - [fromDeviceId] 发送方设备 ID
/// - [toDeviceId]   接收方设备 ID
/// - [deviceType]   设备类型：CONTROLLER / CONTROLLED
/// - [payload]      JSON 字符串形式的负载（SDP / ICE 候选等）
/// - [authType]     鉴权类型：CODE（动态验证码）/ PASSWORD（固定密码）
/// - [authValue]    鉴权值：动态验证码或固定密码
@freezed
class SignalMessage with _$SignalMessage {
  const SignalMessage._();

  const factory SignalMessage({
    String? type,
    String? fromDeviceId,
    String? toDeviceId,
    String? deviceType,
    String? payload,
    String? authType,
    String? authValue,
  }) = _SignalMessage;

  factory SignalMessage.fromJson(Map<String, dynamic> json) =>
      _$SignalMessageFromJson(json);

  /// 便捷构造方法：payload 为任意 Map，会自动序列化为 JSON 字符串。
  factory SignalMessage.withPayload({
    required String type,
    required String fromDeviceId,
    required String toDeviceId,
    required String deviceType,
    required Map<String, dynamic> payload,
    String? authType,
    String? authValue,
  }) {
    return SignalMessage(
      type: type,
      fromDeviceId: fromDeviceId,
      toDeviceId: toDeviceId,
      deviceType: deviceType,
      payload: jsonEncode(payload),
      authType: authType,
      authValue: authValue,
    );
  }

  @override
  String toString() => jsonEncode(toJson());
}