import type {
  GetPrivateZonePostsInput,
  IPrivateZonePostRepository,
} from "@/data/repositories/interfaces";
import {
  UnlockPrivateZonePostRequestSchema,
  type PrivateZonePostsResponse,
  type PrivateZonePostUnlockResponse,
} from "@/data/schemas/private-zone";
import {
  PrivateZonePostApi,
  privateZonePostApi,
} from "@/data/services/api/private_zone_post_api";
import { Result } from "@/utils/result";

import { createLazySingleton } from "./lazy_singleton";

export class PrivateZonePostRepository implements IPrivateZonePostRepository {
  constructor(private readonly api: PrivateZonePostApi) {}

  getPosts(
    input: GetPrivateZonePostsInput,
  ): Promise<Result<PrivateZonePostsResponse>> {
    return Result.wrap(() => this.api.getPosts(input));
  }

  unlockPost(
    postId: string,
    expectedCost: number,
  ): Promise<Result<PrivateZonePostUnlockResponse>> {
    return Result.wrap(() =>
      this.api.unlockPost(
        postId,
        UnlockPrivateZonePostRequestSchema.parse({ expectedCost }),
      ),
    );
  }
}

export const getPrivateZonePostRepository =
  createLazySingleton<IPrivateZonePostRepository>(
    () => new PrivateZonePostRepository(privateZonePostApi),
  );
