"use client";

import {
  UnlockPrivateRoomMomentRequest,
  type PrivateRoomMomentsResponse,
  type PrivateRoomUnlockResponse,
} from "@/data/dto/private-room";
import {
  PrivateRoomApi,
  privateRoomApi,
} from "@/data/services/api/private_room_api";
import type {
  GetPrivateRoomMomentsInput,
  IPrivateRoomRepository,
} from "@/data/repositories/interfaces";
import { Result } from "@/utils/result";

import { createLazySingleton } from "./lazy_singleton";

export class PrivateRoomRepository implements IPrivateRoomRepository {
  constructor(private readonly api: PrivateRoomApi) {}

  getMoments(
    input: GetPrivateRoomMomentsInput = {},
  ): Promise<Result<PrivateRoomMomentsResponse>> {
    return Result.wrap(() => this.api.getMoments(input));
  }

  unlockMoment(
    momentId: string,
    expectedCost: number,
  ): Promise<Result<PrivateRoomUnlockResponse>> {
    return Result.wrap(() =>
      this.api.unlockMoment(
        momentId,
        UnlockPrivateRoomMomentRequest.from({ expectedCost }),
      ),
    );
  }
}

export const getPrivateRoomRepository =
  createLazySingleton<IPrivateRoomRepository>(
    () => new PrivateRoomRepository(privateRoomApi),
  );
