"use client";

/**
 * PaymentRepository
 *
 * 支付 / 付费墙相关远程调用。
 */
import {
  CreatePaymentOrderRequest,
  CreatePaymentOrderResponse,
  PayChannel,
  PaymentOrderStatusResponse,
  PaymentPlansResponse,
  PaymentVipStatusResponse,
} from "@/data/dto/payment";
import { PaymentApi, paymentApi } from "@/data/services/api";
import type { IPaymentRepository } from "@/data/repositories/interfaces";
import { Result } from "@/utils/result";

export class PaymentRepository implements IPaymentRepository {
  constructor(private readonly api: PaymentApi) {}

  /** 获取套餐列表。 */
  async getPlans(): Promise<Result<PaymentPlansResponse>> {
    return Result.wrap(() => this.api.getPlans());
  }

  /** 创建支付订单。 */
  async createOrder(
    planId: string,
    payChannel: PayChannel,
    autoRenew: boolean,
  ): Promise<Result<CreatePaymentOrderResponse>> {
    const request = CreatePaymentOrderRequest.from({
      planId,
      payChannel,
      autoRenew,
    });
    return Result.wrap(() => this.api.createOrder(request));
  }

  /** 查询支付订单状态。 */
  async getOrderStatus(
    orderId: string,
  ): Promise<Result<PaymentOrderStatusResponse>> {
    return Result.wrap(() => this.api.getOrderStatus(orderId));
  }

  /** 查询当前用户 VIP 状态。 */
  async getVipStatus(): Promise<Result<PaymentVipStatusResponse>> {
    return Result.wrap(() => this.api.getVipStatus());
  }
}

/** 全局单例。 */
export const paymentRepository = new PaymentRepository(paymentApi);
