import { http } from "@/utils/http";
import {
  USE_MOCK,
  mockStatisticsOverview,
  mockCategoryOverview,
  mockCategoryTrend,
  mockProductList,
  mockProductTop,
  mockProductTrend,
  mockDeviceOverview,
  mockDeviceList,
  mockDeviceTrend,
  mockShopOverview,
  mockShopList,
  mockShopRanking,
  mockShopTrend,
  mockProfitOverview,
  mockProfitList,
  mockProfitTrend,
  mockProfitWarning,
  mockRepurchaseOverview,
  mockRepurchaseDistribution,
  mockRepurchaseTrend,
  mockRepurchaseUsers
} from "./statisticsMock";

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

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

const mockDelay = (ms: number = 300) => new Promise(resolve => setTimeout(resolve, ms));

export const getStatisticsOverview = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockStatisticsOverview() };
  }
  return http.request<Result>("get", "/statistics/overview", { params });
};

export const getCategoryOverview = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockCategoryOverview() };
  }
  return http.request<Result>("get", "/statistics/category/overview", { params });
};

export const getCategoryTrend = async (params: {
  startDate: string;
  endDate: string;
  category?: string;
  shopId?: number;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockCategoryTrend() };
  }
  return http.request<Result>("get", "/statistics/category/trend", { params });
};

export const getProductList = async (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  deviceId?: string;
  category?: string;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProductList(params.page, params.pageSize) };
  }
  return http.request<ResultTable>("get", "/statistics/product/list", { params });
};

export const getProductTop = async (params: {
  startDate: string;
  endDate: string;
  type: string;
  limit?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProductTop(params.type) };
  }
  return http.request<Result>("get", "/statistics/product/top", { params });
};

export const getProductTrend = async (productId: number, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProductTrend() };
  }
  return http.request<Result>("get", `/statistics/product/${productId}/trend`, { params });
};

export const getDeviceOverview = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockDeviceOverview() };
  }
  return http.request<Result>("get", "/statistics/device/overview", { params });
};

export const getDeviceList = async (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  status?: number;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockDeviceList(params.page, params.pageSize) };
  }
  return http.request<ResultTable>("get", "/statistics/device/list", { params });
};

export const getDeviceTrend = async (deviceId: string, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockDeviceTrend() };
  }
  return http.request<Result>("get", `/statistics/device/${deviceId}/trend`, { params });
};

export const getShopOverview = async (params: {
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockShopOverview() };
  }
  return http.request<Result>("get", "/statistics/shop/overview", { params });
};

export const getShopList = async (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
  district?: string;
  status?: number;
  keyword?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockShopList(params.page, params.pageSize) };
  }
  return http.request<ResultTable>("get", "/statistics/shop/list", { params });
};

export const getShopRanking = async (params: {
  startDate: string;
  endDate: string;
  type: string;
  limit?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockShopRanking() };
  }
  return http.request<Result>("get", "/statistics/shop/ranking", { params });
};

export const getShopTrend = async (shopId: number, params: {
  startDate: string;
  endDate: string;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockShopTrend() };
  }
  return http.request<Result>("get", `/statistics/shop/${shopId}/trend`, { params });
};

export const getProfitOverview = async (params: {
  startDate: string;
  endDate: string;
  compareType?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProfitOverview() };
  }
  return http.request<Result>("get", "/statistics/profit/overview", { params });
};

export const getProfitList = async (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  province?: string;
  city?: string;
  shopId?: number;
  compareType?: string;
  sortBy?: string;
  sortOrder?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProfitList(params.page, params.pageSize) };
  }
  return http.request<ResultTable>("get", "/statistics/profit/list", { params });
};

export const getProfitTrend = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProfitTrend() };
  }
  return http.request<Result>("get", "/statistics/profit/trend", { params });
};

export const getProfitWarning = async (params: {
  startDate: string;
  endDate: string;
  type: string;
  threshold?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockProfitWarning() };
  }
  return http.request<Result>("get", "/statistics/profit/warning", { params });
};

export const getRepurchaseOverview = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockRepurchaseOverview() };
  }
  return http.request<Result>("get", "/statistics/repurchase/overview", { params });
};

export const getRepurchaseDistribution = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  type: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockRepurchaseDistribution(params.type) };
  }
  return http.request<Result>("get", "/statistics/repurchase/distribution", { params });
};

export const getRepurchaseTrend = async (params: {
  startDate: string;
  endDate: string;
  shopId?: number;
  period?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockRepurchaseTrend() };
  }
  return http.request<Result>("get", "/statistics/repurchase/trend", { params });
};

export const getRepurchaseUsers = async (params: {
  page: number;
  pageSize: number;
  startDate: string;
  endDate: string;
  shopId?: number;
  userLayer?: string;
  minOrderCount?: number;
  sortBy?: string;
  sortOrder?: string;
}) => {
  if (USE_MOCK) {
    await mockDelay();
    return { code: 200, message: "success", data: mockRepurchaseUsers(params.page, params.pageSize) };
  }
  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" 
  });
};
