/**
 * 微信支付分相关 API
 */

import { get, post } from '../utils/request';
import { API_PATHS } from '../utils/config';

/**
 * 预创建支付分服务订单（需确认模式，开门前调用）
 */
export interface PreCreatePayscoreOrderRequest {
  deviceId: string;
}

export interface PreCreatePayscoreOrderResponse {
  success: boolean;
  outOrderNo: string;
  package: string;
  mchId?: string;
  errorMsg?: string;
}

export const preCreatePayscoreOrder = (params: PreCreatePayscoreOrderRequest): Promise<PreCreatePayscoreOrderResponse> => {
  return post<PreCreatePayscoreOrderResponse>(API_PATHS.preCreatePayscoreOrder, params);
};

/**
 * 创建支付分服务订单
 */
export interface CreatePayscoreOrderRequest {
  orderId: number;
  openId: string;
  riskFundAmount?: string;
  riskFundName?: string;
}

export interface CreatePayscoreOrderResponse {
  outOrderNo: string;
  state: string;
  package: string;
}

export const createPayscoreOrder = (params: CreatePayscoreOrderRequest): Promise<CreatePayscoreOrderResponse> => {
  return post<CreatePayscoreOrderResponse>(API_PATHS.createPayscoreOrder, params);
};

/**
 * 修改支付分订单金额
 */
export interface ModifyPayscoreOrderRequest {
  orderId: number;
  totalAmount: number;
  payments?: Array<{ name: string; amount: number; description?: string; count?: number }>;
  reason?: string;
}

export const modifyPayscoreOrder = (params: ModifyPayscoreOrderRequest): Promise<{ outOrderNo: string; state: string }> => {
  return post<{ outOrderNo: string; state: string }>(API_PATHS.modifyPayscoreOrder, params);
};

/**
 * 同步订单支付信息（用户已通过其他方式支付）
 */
export const syncPayscoreOrderInfo = (orderId: number, detail?: string): Promise<{ outOrderNo: string; state: string }> => {
  return post<{ outOrderNo: string; state: string }>(API_PATHS.syncPayscoreOrderInfo(orderId), { detail });
};

/**
 * 支付分退款
 */
export interface RefundPayscoreOrderRequest {
  refundAmount?: number;
  reason?: string;
}

export const refundPayscoreOrder = (orderId: number, params: RefundPayscoreOrderRequest): Promise<{ outOrderNo: string; refundId: string }> => {
  return post<{ outOrderNo: string; refundId: string }>(API_PATHS.refundPayscoreOrder(orderId), params);
};

/**
 * 查询支付分退款状态
 */
export const queryPayscoreRefund = (orderId: number, outRefundNo: string): Promise<{ outOrderNo: string; status: string; amount: number }> => {
  return get<{ outOrderNo: string; status: string; amount: number }>(API_PATHS.queryPayscoreRefund(orderId, outRefundNo));
};

/**
 * 按 outOrderNo 查询支付分订单状态（授权返回后判断用户确认还是取消）
 */
export const queryPayscoreByOutNo = (outOrderNo: string): Promise<{ outOrderNo: string; state: string }> => {
  return get<{ outOrderNo: string; state: string }>(API_PATHS.queryPayscoreByOutNo(outOrderNo), undefined, { silent: true });
};

/**
 * 获取微信支付分订单详情签名参数（用于 wx.openBusinessView）
 */
export interface PayscoreDetailSignRequest {
  outOrderNo: string;
}

export interface PayscoreDetailSignResponse {
  mchId: string;
  serviceId: string;
  outOrderNo: string;
  timestamp: string;
  nonceStr: string;
  signType: string;
  sign: string;
}

export const getPayscoreDetailSign = (params: PayscoreDetailSignRequest): Promise<PayscoreDetailSignResponse> => {
  return post<PayscoreDetailSignResponse>(API_PATHS.getPayscoreDetailSign, params);
};
