/**
 * 聊天历史中的单条消息 DTO
 */
import {
  ChatMessageSchema,
  type ChatMessageInput,
  type ChatMessageData,
} from "@/data/schemas/chat/chat_message";

export class ChatMessage {
  declare readonly role: string;
  declare readonly content: string;
  declare readonly id: string;
  declare readonly createdAt: string;
  declare readonly isPrivate: boolean | null;
  declare readonly privateLocked: boolean | null;
  declare readonly privateHint: string | null;

  private constructor(input: ChatMessageInput) {
    const data = ChatMessageSchema.parse(input);
    Object.assign(this, data);
    Object.freeze(this);
  }

  static from(input: ChatMessageInput): ChatMessage {
    return new ChatMessage(input);
  }

  static fromJson(json: unknown): ChatMessage {
    return ChatMessage.from(json as ChatMessageInput);
  }

  toJson(): ChatMessageData {
    return ChatMessageSchema.parse(this);
  }
}
