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

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

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

/** 秒杀活动类型。 */
export type MarketingSeckillActivityType = 'hourly' | 'timed';

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

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

/** 场次。 */
export interface MarketingSeckillSessionDto {
  durationMinutes: number;
  startTime: string;
}

/** 秒杀商品。 */
export interface MarketingSeckillProductDto {
  categoryId: string;
  categoryName: string;
  name: string;
  originalPrice: number;
  perUserLimit: null | number;
  productId: string;
  seckillPrice: number;
  soldCount: number;
  spuCode: string;
  status: MarketingSeckillProductStatus;
  stockLimit: number;
}

/** 活动指标。 */
export interface MarketingSeckillMetricsDto {
  conversionRate: number;
  dealCount: number;
  monthlySeckillSalesCount: number;
  participantCount: number;
}

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

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

/** 保存请求。 */
export interface SaveMarketingSeckillDto {
  activityType: MarketingSeckillActivityType;
  channels: MarketingSeckillChannel[];
  endDate?: string;
  id?: string;
  metrics?: MarketingSeckillMetricsDto;
  name: string;
  perUserLimit: null | number;
  preheatEnabled: boolean;
  preheatHours: null | number;
  products: Array<{
    perUserLimit: null | number;
    productId: string;
    seckillPrice: number;
    stockLimit: number;
  }>;
  sessions: MarketingSeckillSessionDto[];
  startDate?: string;
  storeId: string;
  timeEnd?: string;
  timeStart?: string;
}

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

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

/** 列表统计。 */
export interface MarketingSeckillStatsDto {
  conversionRate: number;
  monthlySeckillSalesCount: number;
  ongoingCount: number;
  totalCount: number;
}

/** 列表项。 */
export interface MarketingSeckillListItemDto {
  activityType: MarketingSeckillActivityType;
  channels: MarketingSeckillChannel[];
  displayStatus: MarketingSeckillDisplayStatus;
  endDate?: string;
  id: string;
  isDimmed: boolean;
  metrics: MarketingSeckillMetricsDto;
  name: string;
  perUserLimit: null | number;
  preheatEnabled: boolean;
  preheatHours: null | number;
  products: MarketingSeckillProductDto[];
  sessions: MarketingSeckillSessionDto[];
  startDate?: string;
  status: MarketingSeckillEditorStatus;
  storeIds: string[];
  timeEnd?: string;
  timeStart?: string;
  updatedAt: string;
}

/** 列表结果。 */
export interface MarketingSeckillListResultDto {
  items: MarketingSeckillListItemDto[];
  page: number;
  pageSize: number;
  stats: MarketingSeckillStatsDto;
  total: number;
}

/** 详情数据。 */
export interface MarketingSeckillDetailDto {
  activityType: MarketingSeckillActivityType;
  channels: MarketingSeckillChannel[];
  displayStatus: MarketingSeckillDisplayStatus;
  endDate?: string;
  id: string;
  metrics: MarketingSeckillMetricsDto;
  name: string;
  perUserLimit: null | number;
  preheatEnabled: boolean;
  preheatHours: null | number;
  products: MarketingSeckillProductDto[];
  sessions: MarketingSeckillSessionDto[];
  startDate?: string;
  status: MarketingSeckillEditorStatus;
  storeIds: string[];
  timeEnd?: string;
  timeStart?: string;
  updatedAt: string;
}

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

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

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

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

/** 获取列表。 */
export async function getMarketingSeckillListApi(
  params: MarketingSeckillListQuery,
) {
  return requestClient.get<MarketingSeckillListResultDto>(
    '/marketing/seckill/list',
    {
      params,
    },
  );
}

/** 获取详情。 */
export async function getMarketingSeckillDetailApi(
  params: MarketingSeckillDetailQuery,
) {
  return requestClient.get<MarketingSeckillDetailDto>(
    '/marketing/seckill/detail',
    {
      params,
    },
  );
}

/** 保存活动。 */
export async function saveMarketingSeckillApi(data: SaveMarketingSeckillDto) {
  return requestClient.post<MarketingSeckillDetailDto>(
    '/marketing/seckill/save',
    data,
  );
}

/** 修改状态。 */
export async function changeMarketingSeckillStatusApi(
  data: ChangeMarketingSeckillStatusDto,
) {
  return requestClient.post<MarketingSeckillDetailDto>(
    '/marketing/seckill/status',
    data,
  );
}

/** 删除活动。 */
export async function deleteMarketingSeckillApi(
  data: DeleteMarketingSeckillDto,
) {
  return requestClient.post('/marketing/seckill/delete', data);
}

/** 获取选品分类。 */
export async function getMarketingSeckillPickerCategoriesApi(
  params: MarketingSeckillPickerCategoryQuery,
) {
  return requestClient.get<MarketingSeckillPickerCategoryItemDto[]>(
    '/marketing/seckill/picker/categories',
    {
      params,
    },
  );
}

/** 获取选品商品。 */
export async function getMarketingSeckillPickerProductsApi(
  params: MarketingSeckillPickerProductQuery,
) {
  return requestClient.get<MarketingSeckillPickerProductItemDto[]>(
    '/marketing/seckill/picker/products',
    {
      params,
    },
  );
}
