import {
  PrivateRoomMomentsResponse,
  PrivateRoomUnlockResponse,
  UnlockPrivateRoomMomentRequest,
} from "@/data/dto/private-room";

import { ApiPath } from "./api_path";
import { httpClient } from "./http_client";
import { type ApiEnvelope, unwrap } from "./response_helper";

export interface GetPrivateRoomMomentsInput {
  character?: string;
  limit?: number;
  cursor?: string | null;
}

export class PrivateRoomApi {
  async getMoments(
    input: GetPrivateRoomMomentsInput = {},
  ): Promise<PrivateRoomMomentsResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.privateRoomMoments,
      {
        query: {
          character: input.character ?? "elio",
          limit: input.limit ?? 20,
          ...(input.cursor ? { cursor: input.cursor } : {}),
        },
      },
    );
    return PrivateRoomMomentsResponse.fromJson(unwrap(env));
  }

  async unlockMoment(
    momentId: string,
    body: UnlockPrivateRoomMomentRequest,
  ): Promise<PrivateRoomUnlockResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.privateRoomMomentUnlock(momentId),
      {
        method: "POST",
        body: body.toJson(),
      },
    );
    return PrivateRoomUnlockResponse.fromJson(unwrap(env));
  }
}

export const privateRoomApi = new PrivateRoomApi();
