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 getDeviceInventoryStats = (params: {
  page?: number;
  pageSize?: number;
  deviceId?: string;
  shopName?: string;
  deviceName?: string;
}) => {
  return http.request<ResultTable>("get", "/inventory/device-stats", {
    params
  });
};

export const getInventoryList = (params: {
  page?: number;
  pageSize?: number;
  deviceId?: string;
  productId?: number;
  lowStock?: boolean;
}) => {
  return http.request<ResultTable>("get", "/inventory/list", { params });
};

export const getDeviceInventory = (deviceId: string) => {
  return http.request<Result>("get", `/inventory/device/${deviceId}`);
};

export const getLowStockList = () => {
  return http.request<Result>("get", "/inventory/low-stock");
};

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

export const increaseStock = (data: {
  deviceId: string;
  productId: number;
  productCode: string;
  productName: string;
  quantity: number;
  shelfNum?: number;
  position?: string;
  activityId?: string;
}) => {
  return http.request<Result>("post", "/inventory/increase", { data });
};

export const adjustStock = (data: {
  deviceId: string;
  productId: number;
  newStock: number;
  remark?: string;
}) => {
  return http.request<Result>("post", "/inventory/adjust", { data });
};

export const getInventoryLogs = (params: {
  page?: number;
  pageSize?: number;
  deviceId?: string;
  productId?: number;
  changeType?: number;
}) => {
  return http.request<ResultTable>("get", "/inventory/logs", { params });
};

export const getLogStatistics = (params: {
  deviceId: string;
  startTime?: string;
  endTime?: string;
}) => {
  return http.request<Result>("get", "/inventory/logs/statistics", { params });
};

export const getStockRecords = (params: {
  page?: number;
  pageSize?: number;
  deviceId?: string;
  stockerId?: number;
  status?: number;
}) => {
  return http.request<ResultTable>("get", "/inventory/records", { params });
};

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

export const completeStockRecord = (
  id: number,
  data: { totalItems?: number; totalQuantity?: number; activityId?: string }
) => {
  return http.request<Result>("put", `/inventory/records/${id}/complete`, {
    data
  });
};

export const cancelStockRecord = (id: number) => {
  return http.request<Result>("put", `/inventory/records/${id}/cancel`);
};

export const getStockerStatistics = (params: {
  stockerId: number;
  startTime?: string;
  endTime?: string;
}) => {
  return http.request<Result>("get", "/inventory/records/stocker-statistics", {
    params
  });
};
