import {
  PrivateZonePostsResponseSchema,
  PrivateZonePostUnlockResponseSchema,
  type PrivateZonePostsResponse,
  type PrivateZonePostUnlockResponse,
  type UnlockPrivateZonePostRequest,
} from "@/data/schemas/private-zone";

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

export interface GetPrivateZonePostsInput {
  characterId: string;
  limit?: number;
  cursor?: string | null;
}

export class PrivateZonePostApi {
  async getPosts(
    input: GetPrivateZonePostsInput,
  ): Promise<PrivateZonePostsResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.privateZonePosts,
      {
        query: {
          characterId: input.characterId,
          limit: input.limit ?? 20,
          cursor: input.cursor || undefined,
        },
      },
    );
    return PrivateZonePostsResponseSchema.parse(unwrap(env));
  }

  async unlockPost(
    postId: string,
    body: UnlockPrivateZonePostRequest,
  ): Promise<PrivateZonePostUnlockResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.privateZonePostUnlock(postId),
      { method: "POST", body },
    );
    const parsed = PrivateZonePostUnlockResponseSchema.parse(unwrap(env));
    return parsed.postId ? parsed : { ...parsed, postId };
  }
}

export const privateZonePostApi = new PrivateZonePostApi();
