import {
  PrivateAlbumsResponseSchema,
  type PrivateAlbumsResponseData,
  type PrivateAlbumsResponseInput,
} from "@/data/schemas/private-room";

import { PrivateAlbum } from "../private_album";

export class PrivateAlbumsResponse {
  declare readonly items: PrivateAlbum[];
  declare readonly creditBalance: number;

  private constructor(input: PrivateAlbumsResponseInput) {
    const data = PrivateAlbumsResponseSchema.parse(input);
    Object.assign(this, {
      items: data.items.map((item) => PrivateAlbum.from(item)),
      creditBalance: data.creditBalance,
    });
    Object.freeze(this);
  }

  static from(input: PrivateAlbumsResponseInput): PrivateAlbumsResponse {
    return new PrivateAlbumsResponse(input);
  }

  static fromJson(json: unknown): PrivateAlbumsResponse {
    return PrivateAlbumsResponse.from(json as PrivateAlbumsResponseInput);
  }

  toJson(): PrivateAlbumsResponseData {
    return {
      items: this.items.map((item) => item.toJson()),
      creditBalance: this.creditBalance,
    };
  }
}
