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 getStatisticsOverview = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  return http.request<Result>("get", "/statistics/overview", { params });
};

// ==================== 品类销售统计 ====================

export const getCategoryOverview = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  return http.request<Result>("get", "/statistics/category/overview", { params });
};

export const getCategoryTrend = (params: {
  startDate: string;
  endDate: string;
  category?: string;
  shopId?: number;
  period?: string;
}) => {
  return http.request<Result>("get", "/statistics/category/trend", { params });
};

// ==================== 商品销售统计 ====================

export const getProductList = (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  deviceId?: string;
  category?: string;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  return http.request<ResultTable>("get", "/statistics/product/list", { params });
};

export const getProductTop = (params: {
  startDate: string;
  endDate: string;
  type: string;
  limit?: number;
}) => {
  return http.request<Result>("get", "/statistics/product/top", { params });
};

export const getProductTrend = (productId: number, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  return http.request<Result>("get", `/statistics/product/${productId}/trend`, { params });
};

// ==================== 设备销售统计 ====================

export const getDeviceOverview = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  return http.request<Result>("get", "/statistics/device/overview", { params });
};

export const getDeviceList = (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  status?: number;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  return http.request<ResultTable>("get", "/statistics/device/list", { params });
};

export const getDeviceTrend = (deviceId: string, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  return http.request<Result>("get", `/statistics/device/${deviceId}/trend`, { params });
};

// ==================== 门店销售统计 ====================

export const getShopOverview = (params: {
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
}) => {
  return http.request<Result>("get", "/statistics/shop/overview", { params });
};

export const getShopList = (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
  district?: string;
  status?: number;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  return http.request<ResultTable>("get", "/statistics/shop/list", { params });
};

export const getShopRanking = (params: {
  startDate: string;
  endDate: string;
  type: string;
  limit?: number;
}) => {
  return http.request<Result>("get", "/statistics/shop/ranking", { params });
};

export const getShopTrend = (shopId: number, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  return http.request<Result>("get", `/statistics/shop/${shopId}/trend`, { params });
};

// ==================== 门店利润报表 ====================

export const getProfitOverview = (params: {
  startDate: string;
  endDate: string;
  compareType?: string;
}) => {
  return http.request<Result>("get", "/statistics/profit/overview", { params });
};

export const getProfitList = (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
  shopId?: number;
  compareType?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  return http.request<ResultTable>("get", "/statistics/profit/list", { params });
};

export const getProfitTrend = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  period?: string;
}) => {
  return http.request<Result>("get", "/statistics/profit/trend", { params });
};

export const getProfitWarning = (params: {
  startDate: string;
  endDate: string;
  type: string;
  threshold?: number;
}) => {
  return http.request<Result>("get", "/statistics/profit/warning", { params });
};

// ==================== 用户复购统计 ====================

export const getRepurchaseOverview = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  return http.request<Result>("get", "/statistics/repurchase/overview", { params });
};

export const getRepurchaseDistribution = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  type: string;
}) => {
  return http.request<Result>("get", "/statistics/repurchase/distribution", { params });
};

export const getRepurchaseTrend = (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  period?: string;
}) => {
  return http.request<Result>("get", "/statistics/repurchase/trend", { params });
};

export const getRepurchaseUsers = (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  userLayer?: string;
  minOrderCount?: number;
  sortBy?: string;
  sortOrder?: string;
}) => {
  return http.request<ResultTable>("get", "/statistics/repurchase/users", { params });
};

// ==================== 导出 ====================

export const exportStatistics = (params: {
  type: string;
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  return http.request("get", "/statistics/export", { 
    params, 
    responseType: "blob" 
  });
};
