/**
 * Payment API
 *
 * 套餐目录、订单创建与订单状态接口。
 */
import {
  CreatePaymentOrderRequest,
  CreatePaymentOrderResponse,
  CreatePaymentOrderResponseSchema,
  CommercialOfferResponse,
  CommercialOfferResponseSchema,
  GiftProductsResponse,
  GiftProductsResponseSchema,
  PaymentOrderStatusResponse,
  PaymentOrderStatusResponseSchema,
  PaymentPlansResponse,
  PaymentPlansResponseSchema,
  TipMessageRequest,
  TipMessageResponse,
  TipMessageResponseSchema,
} from "@/data/schemas/payment";

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

export class PaymentApi {
  /** 获取 VIP 与 Top-up 套餐列表。 */
  async getPlans(
    commercialOfferId?: string,
    supportCharacterId?: string,
  ): Promise<PaymentPlansResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(ApiPath.paymentPlans, {
      ...(commercialOfferId || supportCharacterId
        ? {
            query: {
              ...(commercialOfferId ? { commercialOfferId } : {}),
              ...(supportCharacterId ? { supportCharacterId } : {}),
            },
          }
        : {}),
    });
    return PaymentPlansResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }

  /** 用户确认后激活一份后端签发的专属优惠。 */
  async acceptCommercialOffer(offerId: string): Promise<CommercialOfferResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.paymentCommercialOfferAccept(offerId),
      { method: "POST" },
    );
    return CommercialOfferResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }

  /** 一次获取当前角色的完整礼物目录。 */
  async getGiftProducts(characterId: string): Promise<GiftProductsResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.paymentGiftProducts,
      { query: { characterId } },
    );
    return GiftProductsResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }

  /** 创建 VIP、Top-up 或 Tip 订单。 */
  async createOrder(
    body: CreatePaymentOrderRequest,
  ): Promise<CreatePaymentOrderResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.paymentCreateOrder,
      {
        method: "POST",
        body,
      },
    );
    return CreatePaymentOrderResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }

  /** 查询订单最终支付状态。 */
  async getOrderStatus(orderId: string): Promise<PaymentOrderStatusResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.paymentOrderStatus,
      {
        query: { order_id: orderId },
      },
    );
    return PaymentOrderStatusResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }

  /** 获取已支付礼物订单的稳定感谢文案。 */
  async getTipMessage(body: TipMessageRequest): Promise<TipMessageResponse> {
    const env = await httpClient<ApiEnvelope<unknown>>(
      ApiPath.paymentTipMessage,
      {
        method: "POST",
        body,
      },
    );
    return TipMessageResponseSchema.parse(
      unwrap(env) as Record<string, unknown>,
    );
  }
}

/**
 * 全局单例
 */
export const paymentApi = new PaymentApi();
