import { apiRequest } from './api';

export interface SvipPlan {
  id: string;
  name: string;
  price?: number;
  priceCents?: number;
  originalPrice?: number | null;
  days?: number;
  regionId?: string | null;
  desc?: string | null;
  badge?: string | null;
  recommended?: boolean;
}

export interface OrderItem {
  id: string;
  orderNo: string;
  status: string;
  productType?: string | null;
  productName?: string | null;
  amountCents?: number;
  amount?: number;
  refundedAmountCents?: number;
  payMethod?: string | null;
  payProvider?: string | null;
  tradeNo?: string | null;
  planId?: string | null;
  days?: number | null;
  regionId?: string | null;
  paidAt?: string | null;
  createdAt?: string;
  updatedAt?: string;
}

export interface OrderDetail extends OrderItem {
  pricing?: Record<string, unknown> | null;
  payments?: Record<string, unknown>[];
  items?: Record<string, unknown>[];
  couponRedemptions?: Record<string, unknown>[];
}

export interface OrderStatus {
  orderNo: string;
  status: string;
  amountCents?: number;
  amount?: number;
  refundedAmountCents?: number;
  payProvider?: string | null;
  payMethod?: string | null;
  tradeNo?: string | null;
  paidAt?: string | null;
  updatedAt?: string;
  payment?: Record<string, unknown> | null;
}

export interface PaymentCreateResult {
  orderNo: string;
  provider: 'wechat_pay' | 'alipay' | 'manual' | string;
  method: string;
  paymentParams?: Record<string, unknown>;
}

export interface CouponClaimResult {
  valid?: boolean;
  idempotent?: boolean;
  coupon?: {
    code?: string;
    discountType?: string | null;
    discountValue?: number | null;
    discountCents?: number;
    redemptionId?: string | null;
    redemptionStatus?: string | null;
    plan?: {
      id?: string;
      name?: string;
      price?: number;
      priceCents?: number;
      days?: number;
      regionId?: string | null;
    };
  };
  redemption?: {
    id?: string;
    status?: string;
    claimedAt?: string | null;
    usedAt?: string | null;
  };
}

export async function loadSvipPlans(regionId?: string) {
  return apiRequest<{ items?: SvipPlan[] }>('/api/catalog/svip-plans', { query: { regionId }, authMode: 'none' });
}

export async function createOrder(body: {
  planId: string;
  regionId?: string;
  payMethod?: string;
  payProvider?: string;
  quantity?: number;
  couponCode?: string;
  couponRedemptionId?: string;
}) {
  return apiRequest<{ item?: OrderDetail }>('/api/commerce/orders', {
    method: 'POST',
    body,
  });
}

export async function loadOrders(limit = 20) {
  return apiRequest<{ items?: OrderItem[] }>('/api/commerce/orders', { query: { limit } });
}

export async function loadOrderDetail(orderNo: string) {
  return apiRequest<{ item?: OrderDetail }>('/api/commerce/orders/detail', { query: { orderNo } });
}

export async function loadOrderStatus(orderNo: string) {
  return apiRequest<{ item?: OrderStatus }>('/api/commerce/orders/status', { query: { orderNo } });
}

export async function claimCoupon(body: { code: string; planId?: string; regionId?: string }) {
  return apiRequest<CouponClaimResult>('/api/commerce/coupons/claim', {
    method: 'POST',
    body,
  });
}

export async function createPayment(body: {
  orderNo: string;
  provider?: 'wechat_pay' | 'alipay' | 'manual' | string;
  openId?: string;
  returnUrl?: string;
  quitUrl?: string;
}) {
  return apiRequest<{ item?: PaymentCreateResult }>('/api/commerce/payments/create', {
    method: 'POST',
    body,
  });
}

export async function loadEntitlements() {
  return apiRequest<{ items?: Record<string, unknown>[]; summary?: Record<string, unknown> }>('/api/commerce/entitlements');
}

export async function checkActivationCode(code: string, regionId?: string) {
  return apiRequest<{ valid?: boolean; item?: Record<string, unknown>; reasonCode?: string; message?: string }>(
    '/api/commerce/activation-codes/check',
    { method: 'POST', body: { code, regionId }, authMode: 'none' },
  );
}

export async function redeemActivationCode(code: string, regionId?: string) {
  return apiRequest<{ item?: Record<string, unknown>; entitlement?: unknown }>('/api/commerce/activation-codes/redeem', {
    method: 'POST',
    body: { code, regionId },
  });
}
