import {
  UnlockPrivateAlbumRequest,
  type PrivateAlbumsResponse,
  type PrivateAlbumUnlockResponse,
} from "@/data/dto/private-room";
import {
  PrivateRoomApi,
  privateRoomApi,
} from "@/data/services/api/private_room_api";
import type {
  GetPrivateAlbumsInput,
  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) {}

  getAlbums(
    input: GetPrivateAlbumsInput = {},
  ): Promise<Result<PrivateAlbumsResponse>> {
    return Result.wrap(() => this.api.getAlbums(input));
  }

  unlockAlbum(
    albumId: string,
    expectedCost: number,
  ): Promise<Result<PrivateAlbumUnlockResponse>> {
    return Result.wrap(() =>
      this.api.unlockAlbum(
        albumId,
        UnlockPrivateAlbumRequest.from({ expectedCost }),
      ),
    );
  }
}

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