/**
 * 聊天历史响应 DTO
 */
import {
  ChatHistoryResponseSchema,
  type ChatHistoryResponseInput,
  type ChatHistoryResponseData,
} from "@/data/schemas/chat/chat_history_response";
import { ChatMessage } from "./chat_message";

export class ChatHistoryResponse {
  declare readonly messages: ChatMessage[];
  declare readonly total: number;
  declare readonly limit: number;
  declare readonly offset: number;
  declare readonly isVip: boolean;
  declare readonly privateFreeLimit: number;
  declare readonly privateUsedToday: number;
  declare readonly privateCanViewFree: boolean;

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

  static from(input: ChatHistoryResponseInput): ChatHistoryResponse {
    return new ChatHistoryResponse(input);
  }

  static fromJson(json: unknown): ChatHistoryResponse {
    return ChatHistoryResponse.from(json as ChatHistoryResponseInput);
  }

  toJson(): ChatHistoryResponseData {
    return ChatHistoryResponseSchema.parse(this);
  }
}
