import { fromPromise } from "xstate";

import type {
  PrivateAlbumsResponse,
  PrivateAlbumUnlockResponse,
} from "@/data/dto/private-room";
import { getPrivateRoomRepository } from "@/data/repositories/private_room_repository";
import { Result } from "@/utils/result";

import {
  PRIVATE_ROOM_CHARACTER,
  PRIVATE_ROOM_PAGE_SIZE,
} from "../../helper/albums";

export const loadPrivateAlbumsActor =
  fromPromise<PrivateAlbumsResponse>(async () => {
    const result = await getPrivateRoomRepository().getAlbums({
      character: PRIVATE_ROOM_CHARACTER,
      limit: PRIVATE_ROOM_PAGE_SIZE,
    });
    if (Result.isErr(result)) throw result.error;
    return result.data;
  });

export const unlockPrivateAlbumActor = fromPromise<
  PrivateAlbumUnlockResponse,
  { albumId: string; expectedCost: number }
>(async ({ input }) => {
  const result = await getPrivateRoomRepository().unlockAlbum(
    input.albumId,
    input.expectedCost,
  );
  if (Result.isErr(result)) throw result.error;
  return result.data;
});
