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;
  };
};

// 营销统计相关API

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

export const getActivityStatistics = (id: number, params?: {
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<Result>("get", `/marketing/statistics/activity/${id}`, { params });
};

export const getCouponStatistics = (id: number, params?: {
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<Result>("get", `/marketing/statistics/coupon/${id}`, { params });
};

export const getTrendAnalysis = (params: {
  type: string; // activity, coupon, revenue
  period: string; // day, week, month
  startDate?: string;
  endDate?: string;
}) => {
  return http.request<Result>("get", "/marketing/statistics/trend", { params });
};

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

// 优惠券发放相关API

export const getCouponDistributeRecords = (params: {
  page?: number;
  pageSize?: number;
  templateId?: number;
  distributeType?: number;
}) => {
  return http.request<ResultTable>("get", "/marketing/coupon/distribute/list", { params });
};

export const distributeCoupons = (data: {
  templateId: number;
  distributeType: number; // 1-主动领取 2-系统发放 3-定向发放
  targetType: number; // 1-全部用户 2-新用户 3-指定用户
  userIds?: number[]; // 指定用户ID列表
  quantity?: number; // 发放数量
}) => {
  return http.request<Result>("post", "/marketing/coupon/distribute", { data });
};

// 活动详情相关API

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

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

export const getActivityCoupons = (activityId: number) => {
  return http.request<Result>("get", `/marketing/activity/${activityId}/coupons`);
};

// 优惠券详情相关API

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

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