/**
 * 支付套餐
 */
import { z } from "zod";

const PaymentPlanWireSchema = z.object({
  plan_id: z.string(),
  plan_name: z.string(),
  order_type: z.string(),
  amount_cents: z.number(),
  currency: z.string(),
  vip_days: z.number().nullable(),
  dol_amount: z.number().nullable(),
});

export const PaymentPlanSchema = z
  .preprocess((value) => {
    if (!value || typeof value !== "object") return value;
    const data = value as Record<string, unknown>;
    if ("planId" in data) {
      return {
        plan_id: data.planId,
        plan_name: data.planName,
        order_type: data.orderType,
        amount_cents: data.amountCents,
        currency: data.currency,
        vip_days: data.vipDays,
        dol_amount: data.dolAmount,
      };
    }
    return value;
  }, PaymentPlanWireSchema)
  .transform((data) => ({
    planId: data.plan_id,
    planName: data.plan_name,
    orderType: data.order_type,
    amountCents: data.amount_cents,
    currency: data.currency,
    vipDays: data.vip_days,
    dolAmount: data.dol_amount,
  }));

export type PaymentPlanInput = z.input<typeof PaymentPlanSchema>;
export type PaymentPlanData = z.output<typeof PaymentPlanSchema>;
