import type {
  MarketingFullReductionActivityType,
  MarketingFullReductionChannel,
  MarketingFullReductionDisplayStatus,
  MarketingFullReductionGiftScopeType,
  MarketingFullReductionSecondHalfDiscountType,
  MarketingFullReductionStoreScopeMode,
} from '#/api/marketing';
import type {
  FullReductionEditorForm,
  FullReductionFilterForm,
  FullReductionGiftRuleForm,
  FullReductionScopeForm,
  FullReductionSecondHalfRuleForm,
  FullReductionTierForm,
} from '#/views/marketing/full-reduction/types';

/**
 * 文件职责：满减活动页面常量与默认表单构造。
 */

/** 活动类型筛选项。 */
export const FULL_REDUCTION_ACTIVITY_FILTER_OPTIONS: Array<{
  label: string;
  value: '' | MarketingFullReductionActivityType;
}> = [
  { label: '全部类型', value: '' },
  { label: '满减', value: 'reduce' },
  { label: '满赠', value: 'gift' },
  { label: '第二份半价', value: 'second_half' },
];

/** 活动类型单选项。 */
export const FULL_REDUCTION_EDITOR_ACTIVITY_OPTIONS: Array<{
  label: string;
  value: MarketingFullReductionActivityType;
}> = [
  { label: '满减', value: 'reduce' },
  { label: '满赠', value: 'gift' },
  { label: '第二份半价', value: 'second_half' },
];

/** 展示状态筛选项。 */
export const FULL_REDUCTION_STATUS_FILTER_OPTIONS: Array<{
  label: string;
  value: '' | MarketingFullReductionDisplayStatus;
}> = [
  { label: '全部状态', value: '' },
  { label: '进行中', value: 'ongoing' },
  { label: '未开始', value: 'upcoming' },
  { label: '已结束', value: 'ended' },
];

/** 渠道选项。 */
export const FULL_REDUCTION_CHANNEL_OPTIONS: Array<{
  label: string;
  value: MarketingFullReductionChannel;
}> = [
  { label: '外卖', value: 'delivery' },
  { label: '自提', value: 'pickup' },
  { label: '堂食', value: 'dine_in' },
];

/** 门店范围选项。 */
export const FULL_REDUCTION_STORE_SCOPE_OPTIONS: Array<{
  label: string;
  value: MarketingFullReductionStoreScopeMode;
}> = [
  { label: '全部门店', value: 'all' },
  { label: '指定门店', value: 'stores' },
];

/** 满赠赠品范围类型。 */
export const FULL_REDUCTION_GIFT_SCOPE_OPTIONS: Array<{
  label: string;
  value: MarketingFullReductionGiftScopeType;
}> = [
  { label: '同商品（价低者）', value: 'same_lowest' },
  { label: '指定赠品', value: 'specified' },
];

/** 第二份优惠折扣类型。 */
export const FULL_REDUCTION_SECOND_HALF_DISCOUNT_OPTIONS: Array<{
  label: string;
  value: MarketingFullReductionSecondHalfDiscountType;
}> = [
  { label: '5折', value: 'half' },
  { label: '6折', value: 'sixty' },
  { label: '7折', value: 'seventy' },
  { label: '免费', value: 'free' },
];

/** 叠加规则选项。 */
export const FULL_REDUCTION_STACK_OPTIONS: Array<{
  label: string;
  value: boolean;
}> = [
  { label: '不可叠加优惠券', value: false },
  { label: '可叠加优惠券', value: true },
];

/** 活动类型文案。 */
export const FULL_REDUCTION_ACTIVITY_TEXT_MAP: Record<
  MarketingFullReductionActivityType,
  string
> = {
  reduce: '满减',
  gift: '满赠',
  second_half: '第二份半价',
};

/** 活动类型标签样式类。 */
export const FULL_REDUCTION_ACTIVITY_TAG_CLASS_MAP: Record<
  MarketingFullReductionActivityType,
  string
> = {
  reduce: 'mfr-type-reduce',
  gift: 'mfr-type-gift',
  second_half: 'mfr-type-second-half',
};

/** 活动状态文案。 */
export const FULL_REDUCTION_STATUS_TEXT_MAP: Record<
  MarketingFullReductionDisplayStatus,
  string
> = {
  ongoing: '进行中',
  upcoming: '未开始',
  ended: '已结束',
};

/** 活动状态徽标样式类。 */
export const FULL_REDUCTION_STATUS_TAG_CLASS_MAP: Record<
  MarketingFullReductionDisplayStatus,
  string
> = {
  ongoing: 'mfr-status-ongoing',
  upcoming: 'mfr-status-upcoming',
  ended: 'mfr-status-ended',
};

/** 渠道文案。 */
export const FULL_REDUCTION_CHANNEL_TEXT_MAP: Record<
  MarketingFullReductionChannel,
  string
> = {
  delivery: '外卖',
  pickup: '自提',
  dine_in: '堂食',
};

/** 构建默认筛选表单。 */
export function createDefaultFullReductionFilterForm(): FullReductionFilterForm {
  return {
    activityType: '',
    status: '',
  };
}

/** 构建空指标对象。 */
export function createEmptyFullReductionMetrics() {
  return {
    participatingOrderCount: 0,
    discountTotalAmount: 0,
    ticketIncreaseAmount: 0,
    giftedCount: 0,
    drivenSalesAmount: 0,
    attachRateIncreasePercent: 0,
    monthlyDrivenSalesAmount: 0,
    averageTicketIncrease: 0,
  };
}

/** 构建默认商品范围。 */
export function createDefaultScopeForm(
  scopeType: FullReductionScopeForm['scopeType'] = 'all',
): FullReductionScopeForm {
  return {
    scopeType,
    categoryIds: [],
    productIds: [],
  };
}

/** 构建默认阶梯行。 */
export function createDefaultTierForm(
  meetAmount: null | number = null,
  reduceAmount: null | number = null,
): FullReductionTierForm {
  return {
    meetAmount,
    reduceAmount,
  };
}

/** 构建默认满赠规则。 */
export function createDefaultGiftRuleForm(): FullReductionGiftRuleForm {
  return {
    buyQuantity: 2,
    giftQuantity: 1,
    giftScopeType: 'same_lowest',
    applicableScope: createDefaultScopeForm('all'),
    giftScope: createDefaultScopeForm('all'),
  };
}

/** 构建默认第二份半价规则。 */
export function createDefaultSecondHalfRuleForm(): FullReductionSecondHalfRuleForm {
  return {
    discountType: 'half',
    applicableScope: createDefaultScopeForm('category'),
  };
}

/** 构建默认编辑表单。 */
export function createDefaultFullReductionEditorForm(): FullReductionEditorForm {
  return {
    id: '',
    name: '',
    activityType: 'reduce',
    reduceTiers: [createDefaultTierForm(30, 5)],
    giftRule: createDefaultGiftRuleForm(),
    secondHalfRule: createDefaultSecondHalfRuleForm(),
    validDateRange: null,
    channels: ['delivery', 'pickup', 'dine_in'],
    storeScopeMode: 'all',
    storeIds: [],
    scopeStoreId: '',
    stackWithCoupon: false,
    description: '',
    metrics: createEmptyFullReductionMetrics(),
  };
}
