/**
 * 文件职责：营销中心限时折扣 API 与 DTO 定义。
 * 1. 维护限时折扣列表、详情、保存、状态切换、删除和选品契约。
 */
import { requestClient } from '#/api/request';

/** 活动展示状态。 */
export type MarketingFlashSaleDisplayStatus = 'ended' | 'ongoing' | 'upcoming';

/** 活动编辑状态。 */
export type MarketingFlashSaleEditorStatus = 'active' | 'completed';

/** 活动周期。 */
export type MarketingFlashSaleCycleType = 'once' | 'recurring';

/** 周期日期模式。 */
export type MarketingFlashSaleRecurringDateMode = 'fixed' | 'long_term';

/** 适用渠道。 */
export type MarketingFlashSaleChannel = 'delivery' | 'dine_in' | 'pickup';

/** 商品状态。 */
export type MarketingFlashSaleProductStatus =
  | 'off_shelf'
  | 'on_sale'
  | 'sold_out';

/** 限时折扣商品。 */
export interface MarketingFlashSaleProductDto {
  categoryId: string;
  categoryName: string;
  discountPrice: number;
  name: string;
  originalPrice: number;
  perUserLimit: null | number;
  productId: string;
  soldCount: number;
  spuCode: string;
  status: MarketingFlashSaleProductStatus;
}

/** 活动指标。 */
export interface MarketingFlashSaleMetricsDto {
  activitySalesCount: number;
  discountTotalAmount: number;
  loopedWeeks: number;
  monthlyDiscountSalesCount: number;
}

/** 列表查询参数。 */
export interface MarketingFlashSaleListQuery {
  keyword?: string;
  page: number;
  pageSize: number;
  status?: '' | MarketingFlashSaleDisplayStatus;
  storeId?: string;
}

/** 详情查询参数。 */
export interface MarketingFlashSaleDetailQuery {
  activityId: string;
  storeId: string;
}

/** 保存请求。 */
export interface SaveMarketingFlashSaleDto {
  channels: MarketingFlashSaleChannel[];
  cycleType: MarketingFlashSaleCycleType;
  endDate?: string;
  id?: string;
  metrics?: MarketingFlashSaleMetricsDto;
  name: string;
  perUserLimit: null | number;
  products: Array<{
    discountPrice: number;
    perUserLimit: null | number;
    productId: string;
  }>;
  recurringDateMode: MarketingFlashSaleRecurringDateMode;
  startDate?: string;
  storeId: string;
  storeIds: string[];
  timeEnd?: string;
  timeStart?: string;
  weekDays: number[];
}

/** 状态修改请求。 */
export interface ChangeMarketingFlashSaleStatusDto {
  activityId: string;
  status: MarketingFlashSaleEditorStatus;
  storeId: string;
}

/** 删除请求。 */
export interface DeleteMarketingFlashSaleDto {
  activityId: string;
  storeId: string;
}

/** 列表统计。 */
export interface MarketingFlashSaleStatsDto {
  monthlyDiscountSalesCount: number;
  ongoingCount: number;
  participatingProductCount: number;
  totalCount: number;
}

/** 列表项。 */
export interface MarketingFlashSaleListItemDto {
  channels: MarketingFlashSaleChannel[];
  cycleType: MarketingFlashSaleCycleType;
  displayStatus: MarketingFlashSaleDisplayStatus;
  endDate?: string;
  id: string;
  isDimmed: boolean;
  metrics: MarketingFlashSaleMetricsDto;
  name: string;
  perUserLimit: null | number;
  products: MarketingFlashSaleProductDto[];
  recurringDateMode: MarketingFlashSaleRecurringDateMode;
  startDate?: string;
  status: MarketingFlashSaleEditorStatus;
  storeIds: string[];
  timeEnd?: string;
  timeStart?: string;
  updatedAt: string;
  weekDays: number[];
}

/** 列表结果。 */
export interface MarketingFlashSaleListResultDto {
  items: MarketingFlashSaleListItemDto[];
  page: number;
  pageSize: number;
  stats: MarketingFlashSaleStatsDto;
  total: number;
}

/** 详情数据。 */
export interface MarketingFlashSaleDetailDto {
  channels: MarketingFlashSaleChannel[];
  cycleType: MarketingFlashSaleCycleType;
  displayStatus: MarketingFlashSaleDisplayStatus;
  endDate?: string;
  id: string;
  metrics: MarketingFlashSaleMetricsDto;
  name: string;
  perUserLimit: null | number;
  products: MarketingFlashSaleProductDto[];
  recurringDateMode: MarketingFlashSaleRecurringDateMode;
  startDate?: string;
  status: MarketingFlashSaleEditorStatus;
  storeIds: string[];
  timeEnd?: string;
  timeStart?: string;
  updatedAt: string;
  weekDays: number[];
}

/** 选品分类查询参数。 */
export interface MarketingFlashSalePickerCategoryQuery {
  storeId: string;
}

/** 选品分类项。 */
export interface MarketingFlashSalePickerCategoryItemDto {
  id: string;
  name: string;
  productCount: number;
}

/** 选品商品查询参数。 */
export interface MarketingFlashSalePickerProductQuery {
  categoryId?: string;
  keyword?: string;
  limit?: number;
  storeId: string;
}

/** 选品商品项。 */
export interface MarketingFlashSalePickerProductItemDto {
  categoryId: string;
  categoryName: string;
  id: string;
  name: string;
  price: number;
  spuCode: string;
  status: MarketingFlashSaleProductStatus;
  stock: number;
}

/** 获取列表。 */
export async function getMarketingFlashSaleListApi(
  params: MarketingFlashSaleListQuery,
) {
  return requestClient.get<MarketingFlashSaleListResultDto>(
    '/marketing/flash-sale/list',
    {
      params,
    },
  );
}

/** 获取详情。 */
export async function getMarketingFlashSaleDetailApi(
  params: MarketingFlashSaleDetailQuery,
) {
  return requestClient.get<MarketingFlashSaleDetailDto>(
    '/marketing/flash-sale/detail',
    {
      params,
    },
  );
}

/** 保存活动。 */
export async function saveMarketingFlashSaleApi(
  data: SaveMarketingFlashSaleDto,
) {
  return requestClient.post<MarketingFlashSaleDetailDto>(
    '/marketing/flash-sale/save',
    data,
  );
}

/** 修改状态。 */
export async function changeMarketingFlashSaleStatusApi(
  data: ChangeMarketingFlashSaleStatusDto,
) {
  return requestClient.post<MarketingFlashSaleDetailDto>(
    '/marketing/flash-sale/status',
    data,
  );
}

/** 删除活动。 */
export async function deleteMarketingFlashSaleApi(
  data: DeleteMarketingFlashSaleDto,
) {
  return requestClient.post('/marketing/flash-sale/delete', data);
}

/** 获取选品分类。 */
export async function getMarketingFlashSalePickerCategoriesApi(
  params: MarketingFlashSalePickerCategoryQuery,
) {
  return requestClient.get<MarketingFlashSalePickerCategoryItemDto[]>(
    '/marketing/flash-sale/picker/categories',
    {
      params,
    },
  );
}

/** 获取选品商品。 */
export async function getMarketingFlashSalePickerProductsApi(
  params: MarketingFlashSalePickerProductQuery,
) {
  return requestClient.get<MarketingFlashSalePickerProductItemDto[]>(
    '/marketing/flash-sale/picker/products',
    {
      params,
    },
  );
}
