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

/** 活动类型。 */
export type MarketingFullReductionActivityType =
  | 'gift'
  | 'reduce'
  | 'second_half';

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

/** 编辑状态。 */
export type MarketingFullReductionEditorStatus = 'active' | 'completed';

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

/** 门店范围。 */
export type MarketingFullReductionStoreScopeMode = 'all' | 'stores';

/** 商品范围。 */
export type MarketingFullReductionScopeType = 'all' | 'category' | 'product';

/** 满赠赠品类型。 */
export type MarketingFullReductionGiftScopeType = 'same_lowest' | 'specified';

/** 第二份折扣类型。 */
export type MarketingFullReductionSecondHalfDiscountType =
  | 'free'
  | 'half'
  | 'seventy'
  | 'sixty';

/** 满减阶梯规则。 */
export interface MarketingFullReductionTierRuleDto {
  meetAmount: number;
  reduceAmount: number;
}

/** 商品范围规则。 */
export interface MarketingFullReductionScopeRuleDto {
  categoryIds: string[];
  productIds: string[];
  scopeType: MarketingFullReductionScopeType;
}

/** 满赠规则。 */
export interface MarketingFullReductionGiftRuleDto {
  applicableScope: MarketingFullReductionScopeRuleDto;
  buyQuantity: number;
  giftQuantity: number;
  giftScope: MarketingFullReductionScopeRuleDto;
  giftScopeType: MarketingFullReductionGiftScopeType;
}

/** 第二份半价规则。 */
export interface MarketingFullReductionSecondHalfRuleDto {
  applicableScope: MarketingFullReductionScopeRuleDto;
  discountType: MarketingFullReductionSecondHalfDiscountType;
}

/** 活动指标。 */
export interface MarketingFullReductionMetricsDto {
  attachRateIncreasePercent: number;
  averageTicketIncrease: number;
  discountTotalAmount: number;
  drivenSalesAmount: number;
  giftedCount: number;
  monthlyDrivenSalesAmount: number;
  participatingOrderCount: number;
  ticketIncreaseAmount: number;
}

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

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

/** 保存请求。 */
export interface SaveMarketingFullReductionDto {
  activityType: MarketingFullReductionActivityType;
  channels: MarketingFullReductionChannel[];
  description?: string;
  endDate: string;
  giftRule?: MarketingFullReductionGiftRuleDto;
  id?: string;
  metrics?: MarketingFullReductionMetricsDto;
  name: string;
  reduceTiers: MarketingFullReductionTierRuleDto[];
  scopeStoreId: string;
  secondHalfRule?: MarketingFullReductionSecondHalfRuleDto;
  stackWithCoupon: boolean;
  startDate: string;
  storeId: string;
  storeIds?: string[];
  storeScopeMode: MarketingFullReductionStoreScopeMode;
}

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

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

/** 统计数据。 */
export interface MarketingFullReductionStatsDto {
  averageTicketIncrease: number;
  monthlyDrivenSalesAmount: number;
  ongoingCount: number;
  totalCount: number;
}

/** 列表项。 */
export interface MarketingFullReductionListItemDto {
  activityType: MarketingFullReductionActivityType;
  channels: MarketingFullReductionChannel[];
  description?: string;
  displayStatus: MarketingFullReductionDisplayStatus;
  endDate: string;
  giftRule?: MarketingFullReductionGiftRuleDto;
  id: string;
  isDimmed: boolean;
  metrics: MarketingFullReductionMetricsDto;
  name: string;
  reduceTiers: MarketingFullReductionTierRuleDto[];
  scopeStoreId: string;
  secondHalfRule?: MarketingFullReductionSecondHalfRuleDto;
  stackWithCoupon: boolean;
  startDate: string;
  storeIds: string[];
  storeScopeMode: MarketingFullReductionStoreScopeMode;
  updatedAt: string;
}

/** 列表结果。 */
export interface MarketingFullReductionListResultDto {
  items: MarketingFullReductionListItemDto[];
  page: number;
  pageSize: number;
  stats: MarketingFullReductionStatsDto;
  total: number;
}

/** 详情数据。 */
export interface MarketingFullReductionDetailDto {
  activityType: MarketingFullReductionActivityType;
  channels: MarketingFullReductionChannel[];
  description?: string;
  displayStatus: MarketingFullReductionDisplayStatus;
  endDate: string;
  giftRule?: MarketingFullReductionGiftRuleDto;
  id: string;
  metrics: MarketingFullReductionMetricsDto;
  name: string;
  reduceTiers: MarketingFullReductionTierRuleDto[];
  scopeStoreId: string;
  secondHalfRule?: MarketingFullReductionSecondHalfRuleDto;
  stackWithCoupon: boolean;
  startDate: string;
  status: MarketingFullReductionEditorStatus;
  storeIds: string[];
  storeScopeMode: MarketingFullReductionStoreScopeMode;
  updatedAt: string;
}

/** 获取列表。 */
export async function getMarketingFullReductionListApi(
  params: MarketingFullReductionListQuery,
) {
  return requestClient.get<MarketingFullReductionListResultDto>(
    '/marketing/full-reduction/list',
    {
      params,
    },
  );
}

/** 获取详情。 */
export async function getMarketingFullReductionDetailApi(
  params: MarketingFullReductionDetailQuery,
) {
  return requestClient.get<MarketingFullReductionDetailDto>(
    '/marketing/full-reduction/detail',
    {
      params,
    },
  );
}

/** 保存活动。 */
export async function saveMarketingFullReductionApi(
  data: SaveMarketingFullReductionDto,
) {
  return requestClient.post<MarketingFullReductionDetailDto>(
    '/marketing/full-reduction/save',
    data,
  );
}

/** 修改状态。 */
export async function changeMarketingFullReductionStatusApi(
  data: ChangeMarketingFullReductionStatusDto,
) {
  return requestClient.post<MarketingFullReductionDetailDto>(
    '/marketing/full-reduction/status',
    data,
  );
}

/** 删除活动。 */
export async function deleteMarketingFullReductionApi(
  data: DeleteMarketingFullReductionDto,
) {
  return requestClient.post('/marketing/full-reduction/delete', data);
}
