import { http } from "@/utils/http";

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

export const getProductList = (params: {
  page?: number;
  pageSize?: number;
  name?: string;
  barcode?: string;
  category?: string;
  syncStatus?: number;
}) => {
  return http.request<ResultTable>("get", "/products/list", { params });
};

export const getProductById = (id: number) => {
  return http.request<Result>("get", `/products/${id}`);
};

export const getProductStatistics = () => {
  return http.request<Result>("get", "/products/statistics");
};

export const addProduct = (data: any) => {
  return http.request<Result>("post", "/products", { data });
};

export const updateProduct = (id: number, data: any) => {
  return http.request<Result>("put", `/products/${id}`, { data });
};

export const deleteProduct = (id: number) => {
  return http.request<Result>("delete", `/products/${id}`);
};

export const syncProduct = (id: number) => {
  return http.request<Result>("post", `/products/${id}/sync`);
};

// 商品总库
export const getMasterProductList = (params: {
  page?: number;
  pageSize?: number;
  barcode?: string;
  code?: string;
  productName?: string;
  deviceAdapt?: string;
}) => {
  return http.request<ResultTable>("get", "/products/master/list", { params });
};

export const addToMerchantProducts = (data: { codeList: string }) => {
  return http.request<Result>("post", "/products/master/add-to-merchant", { data });
};

// 商品上下架控制
export const setProductShelfStatus = (data: {
  deviceId: string;
  goodsCode: string;
  salesType: string;
  callType?: string;
}) => {
  return http.request<Result>("post", "/products/shelf-status", { data });
};

// 获取指定设备可售卖商品列表
export const getDeviceProducts = (deviceId: string) => {
  return http.request<Result>("get", `/products/device/${deviceId}`);
};

// 查询商品ID对应关系
export const getIdMapping = (codeList?: string) => {
  const params = codeList ? { codeList } : {};
  return http.request<Result>("get", "/products/id-mapping", { params });
};
