/**
 * 消息同步请求 DTO
 */
import {
  ChatSyncRequestSchema,
  type ChatSyncRequestInput,
  type ChatSyncRequestData,
} from "@/data/schemas/chat/chat_sync_request";
import { SyncMessage } from "./sync_message";

export class ChatSyncRequest {
  declare readonly messages: SyncMessage[];

  private constructor(input: ChatSyncRequestInput) {
    const data = ChatSyncRequestSchema.parse(input);
    Object.assign(this, {
      ...data,
      messages: data.messages.map((m) => SyncMessage.from(m)),
    });
    Object.freeze(this);
  }

  static from(input: ChatSyncRequestInput): ChatSyncRequest {
    return new ChatSyncRequest(input);
  }

  static fromJson(json: unknown): ChatSyncRequest {
    return ChatSyncRequest.from(json as ChatSyncRequestInput);
  }

  toJson(): ChatSyncRequestData {
    return ChatSyncRequestSchema.parse(this);
  }
}
