import {
  PrivateRoomMomentsResponseSchema,
  type PrivateRoomMomentsResponseData,
  type PrivateRoomMomentsResponseInput,
} from "@/data/schemas/private-room";

import { PrivateRoomMoment } from "../private_room_moment";

export class PrivateRoomMomentsResponse {
  declare readonly profile: PrivateRoomMomentsResponseData["profile"];
  declare readonly items: PrivateRoomMoment[];
  declare readonly nextCursor: string | null;
  declare readonly hasMore: boolean;
  declare readonly creditBalance: number;
  declare readonly unlockCostDefault: number;
  declare readonly unlockCostPerImage: number;
  declare readonly currency: string;
  declare readonly source: string;

  private constructor(input: PrivateRoomMomentsResponseInput) {
    const data = PrivateRoomMomentsResponseSchema.parse(input);
    Object.assign(this, {
      ...data,
      items: data.items.map((item) => PrivateRoomMoment.from(item)),
    });
    Object.freeze(this);
  }

  static from(
    input: PrivateRoomMomentsResponseInput,
  ): PrivateRoomMomentsResponse {
    return new PrivateRoomMomentsResponse(input);
  }

  static fromJson(json: unknown): PrivateRoomMomentsResponse {
    return PrivateRoomMomentsResponse.from(
      json as PrivateRoomMomentsResponseInput,
    );
  }

  toJson(): PrivateRoomMomentsResponseData {
    return {
      profile: this.profile,
      items: this.items.map((item) => item.toJson()),
      nextCursor: this.nextCursor,
      hasMore: this.hasMore,
      creditBalance: this.creditBalance,
      unlockCostDefault: this.unlockCostDefault,
      unlockCostPerImage: this.unlockCostPerImage,
      currency: this.currency,
      source: this.source,
    };
  }
}
