// 商城服务 - 对应 supermart-mini/service/mall.js
import { get, post, postL } from './http';

const apis = {
  CATEGORY: '/api/spu/basic/listCategoryLimit',
  LIST: '/api/mallGoods',
  DETAIL: '/api/mallGoods/detail',
  DETAIL_BY_SPU: '/api/mallGoods/detailBySpuId',
  PREVIEW_SUBMIT: '/api/mallOrder/preSubmit',
  ORDERS: '/api/mallOrder/pageMy',
  ORDER_DETAIL: '/api/mallOrder/detail',
  APPLY: '/api/mallOrder/submit',
  ORDER_PAY: '/api/mallOrder/pay',
  RECEIVE: '/api/mallOrder/receive',
  PACKAGES: '/api/deliveryOrder/expressInfo',
  EXPRESS: '/api/deliveryOrder/expressDetail',
  UPDATE_PHONE: '/api/mallOrder/updateRestNotifyMobile',
  SPIKE_LIST: '/api/activity/mallSubject/subjectGoodsPage',
  SPIKE_TIMER: '/api/activity/mallSubject/subject',
};

export interface GoodsItem {
  id: string;
  name: string;
  price: number;
  cover: string;
  sellType?: number;
  worksId?: string;
  categoryId?: string;
}

export interface GoodsListParams {
  current: number;
  size: number;
  keyword?: string;
  worksId?: string;
  categoryId?: string;
  sellType?: string;
}

export interface GoodsDetail {
  id: string;
  name: string;
  price: number;
  cover: string;
  sellType?: number;
  sellFlag?: number;
  sellTime?: string;
  deposit?: number;
  subjectPrice?: number;
  spu: {
    id: string;
    name: string;
    cover: string;
    images?: string[];
    description?: string;
  };
  recommendedLuckPool?: any[];
  recommendedMallGoods?: GoodsItem[];
}

export interface PreviewSubmitParams {
  goodsId: string;
  quantity: number;
  subjectId?: string;
}

export interface PreviewSubmitResult {
  paymentAmount: number;
  depositAmount?: number;
  expressAmount?: number;
  couponAmount?: number;
  type?: number;
  cash?: {
    balance: number;
  };
}

export interface OrderSubmitParams {
  goodsId: string;
  paymentType: string;
  addressBookId: string;
  quantity: number;
  restNotifyMobile?: string;
  subjectId?: string;
}

export interface OrderItem {
  tradeNo: string;
  status: number;
  statusText: string;
  goodsName: string;
  goodsCover: string;
  quantity: number;
  paymentAmount: number;
  createTime: string;
  type?: number;
  paidAmount?: number;
  totalAmount?: number;
  couponAmount?: number;
  paymentTimeoutTime?: string;
}

export interface OrderDetail extends OrderItem {
  address?: {
    contactName: string;
    contactNo: string;
    province: string;
    city: string;
    district: string;
    address: string;
  };
}

// 获取商品分类
export const getCategories = async (limit = 5) => {
  const res = await get(apis.CATEGORY, { limit });
  return res.data;
};

// 获取商品列表
export const getGoodsList = async (params: GoodsListParams) => {
  const res = await post<GoodsItem[]>(apis.LIST, params);
  return res.data || [];
};

// 获取商品详情
export const getGoodsDetail = async (goodsId: string, subjectId?: string): Promise<GoodsDetail | null> => {
  const params: any = { goodsId };
  if (subjectId) params.subjectId = subjectId;
  const res = await get<GoodsDetail>(apis.DETAIL, params);
  return res.data;
};

// 通过 SPU ID 获取商品详情
export const getGoodsDetailBySpu = async (spuId: string): Promise<GoodsDetail | null> => {
  const res = await get<GoodsDetail>(apis.DETAIL_BY_SPU, { goodsId: '', spuId });
  return res.data;
};

// 预提交订单（获取价格信息）
export const previewSubmit = async (params: PreviewSubmitParams): Promise<PreviewSubmitResult | null> => {
  const res = await postL<PreviewSubmitResult>(apis.PREVIEW_SUBMIT, params);
  return res.data;
};

// 提交订单
export const submitOrder = async (params: OrderSubmitParams) => {
  const res = await postL(apis.APPLY, params);
  return res.data;
};

// 获取订单列表
export const getOrders = async (current: number, size: number, tab?: string) => {
  const res = await post<{ records: OrderItem[]; total: number }>(apis.ORDERS, { current, size, tab });
  return res.data;
};

// 获取订单详情
export const getOrderDetail = async (tradeNo: string): Promise<OrderDetail | null> => {
  const res = await get<OrderDetail>(apis.ORDER_DETAIL, { tradeNo });
  return res.data;
};

// 订单支付
export const payOrder = async (tradeNo: string, paymentType: string) => {
  const res = await postL(apis.ORDER_PAY, { tradeNo, paymentType });
  return res.data;
};

// 确认收货
export const confirmReceive = async (tradeNo: string) => {
  const res = await postL(apis.RECEIVE, { tradeNo });
  return res.success;
};

// 获取包裹信息
export const getPackages = async (tradeNo: string) => {
  const res = await get(apis.PACKAGES, { tradeNo });
  return res.data;
};

// 获取物流详情
export const getExpress = async (tradeNo: string, packageId: string) => {
  const res = await get(apis.EXPRESS, { tradeNo, packageId });
  return res.data;
};

// 更新尾款提醒手机号
export const updatePhone = async (params: { tradeNo: string; restNotifyMobile: string }) => {
  const res = await postL(apis.UPDATE_PHONE, params);
  return res;
};

// 获取秒杀商品列表
export const getSpikeList = async (params: any) => {
  const res = await postL(apis.SPIKE_LIST, params);
  return res.data;
};

// 获取秒杀活动信息
export const getSpikeTimer = async (subjectId: string) => {
  const res = await get(`${apis.SPIKE_TIMER}/${subjectId}`);
  return res.data;
};

export default {
  getCategories,
  getGoodsList,
  getGoodsDetail,
  getGoodsDetailBySpu,
  previewSubmit,
  submitOrder,
  getOrders,
  getOrderDetail,
  payOrder,
  confirmReceive,
  getPackages,
  getExpress,
  updatePhone,
  getSpikeList,
  getSpikeTimer,
};
