/// 聊天室金鑰 DTO
/// 負責與原生平台傳輸聊天室加密金鑰資料
library;

import '../../domain/entities/chat_encryption_key.dart';
import '../../domain/entities/session_key.dart';

class ChatEncryptionKeyDto {
  final String chatId;
  final String activeKey;  // Base64 編碼的活躍金鑰
  final int activeRound;
  final int round;
  final bool isSingle;

  const ChatEncryptionKeyDto({
    required this.chatId,
    required this.activeKey,
    required this.activeRound,
    required this.round,
    required this.isSingle,
  });

  /// 從 JSON 創建
  factory ChatEncryptionKeyDto.fromJson(Map<String, dynamic> json) {
    return ChatEncryptionKeyDto(
      chatId: json['chatId'] as String,
      activeKey: json['activeKey'] as String,
      activeRound: json['activeRound'] as int,
      round: json['round'] as int,
      isSingle: json['isSingle'] as bool,
    );
  }

  /// 轉換為 JSON
  Map<String, dynamic> toJson() {
    return {
      'chatId': chatId,
      'activeKey': activeKey,
      'activeRound': activeRound,
      'round': round,
      'isSingle': isSingle,
    };
  }

  /// 轉換為 Domain Entity
  ChatEncryptionKey toEntity() {
    return ChatEncryptionKey(
      chatId: chatId,
      sessionKey: SessionKey(key: activeKey, round: activeRound),
      activeKeyRound: activeRound,
      currentRound: round,
      isSingle: isSingle,
    );
  }

  /// 從 Domain Entity 創建
  static ChatEncryptionKeyDto fromEntity(ChatEncryptionKey entity) {
    return ChatEncryptionKeyDto(
      chatId: entity.chatId,
      activeKey: entity.sessionKey.key,
      activeRound: entity.activeKeyRound,
      round: entity.currentRound,
      isSingle: entity.isSingle,
    );
  }
}

