import type {
  MarketingFlashSaleChannel,
  MarketingFlashSaleCycleType,
  MarketingFlashSaleDisplayStatus,
  MarketingFlashSaleProductStatus,
  MarketingFlashSaleRecurringDateMode,
} from '#/api/marketing';
import type {
  FlashSaleEditorForm,
  FlashSaleEditorProductForm,
  FlashSaleFilterForm,
} from '#/views/marketing/flash-sale/types';

/**
 * 文件职责：限时折扣页面常量与默认表单。
 */

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

/** 活动周期选项。 */
export const FLASH_SALE_CYCLE_OPTIONS: Array<{
  label: string;
  value: MarketingFlashSaleCycleType;
}> = [
  { label: '一次性活动', value: 'once' },
  { label: '周期循环', value: 'recurring' },
];

/** 周期日期模式选项。 */
export const FLASH_SALE_RECURRING_DATE_MODE_OPTIONS: Array<{
  label: string;
  value: MarketingFlashSaleRecurringDateMode;
}> = [
  { label: '长期有效', value: 'long_term' },
  { label: '指定日期区间', value: 'fixed' },
];

/** 适用渠道选项。 */
export const FLASH_SALE_CHANNEL_OPTIONS: Array<{
  label: string;
  value: MarketingFlashSaleChannel;
}> = [
  { label: '外卖配送', value: 'delivery' },
  { label: '到店自取', value: 'pickup' },
  { label: '堂食点餐', value: 'dine_in' },
];

/** 周循环星期选项。 */
export const FLASH_SALE_WEEKDAY_OPTIONS: Array<{
  label: string;
  shortLabel: string;
  value: number;
}> = [
  { label: '周一', shortLabel: '一', value: 1 },
  { label: '周二', shortLabel: '二', value: 2 },
  { label: '周三', shortLabel: '三', value: 3 },
  { label: '周四', shortLabel: '四', value: 4 },
  { label: '周五', shortLabel: '五', value: 5 },
  { label: '周六', shortLabel: '六', value: 6 },
  { label: '周日', shortLabel: '日', value: 7 },
];

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

/** 展示状态类。 */
export const FLASH_SALE_STATUS_CLASS_MAP: Record<
  MarketingFlashSaleDisplayStatus,
  string
> = {
  ongoing: 'fs-tag-running',
  upcoming: 'fs-tag-notstarted',
  ended: 'fs-tag-ended',
};

/** 商品状态文案。 */
export const FLASH_SALE_PRODUCT_STATUS_TEXT_MAP: Record<
  MarketingFlashSaleProductStatus,
  string
> = {
  on_sale: '在售',
  off_shelf: '下架',
  sold_out: '沽清',
};

/** 创建默认筛选表单。 */
export function createDefaultFlashSaleFilterForm(): FlashSaleFilterForm {
  return {
    status: '',
  };
}

/** 创建空指标对象。 */
export function createEmptyFlashSaleMetrics() {
  return {
    activitySalesCount: 0,
    discountTotalAmount: 0,
    loopedWeeks: 0,
    monthlyDiscountSalesCount: 0,
  };
}

/** 创建默认商品表单。 */
export function createDefaultFlashSaleProductForm(
  product?: Partial<FlashSaleEditorProductForm>,
): FlashSaleEditorProductForm {
  return {
    productId: product?.productId ?? '',
    categoryId: product?.categoryId ?? '',
    categoryName: product?.categoryName ?? '',
    name: product?.name ?? '',
    spuCode: product?.spuCode ?? '',
    status: product?.status ?? 'off_shelf',
    originalPrice: Number(product?.originalPrice ?? 0),
    discountPrice:
      product?.discountPrice === null || product?.discountPrice === undefined
        ? null
        : Number(product.discountPrice),
    perUserLimit:
      product?.perUserLimit === null || product?.perUserLimit === undefined
        ? null
        : Number(product.perUserLimit),
    soldCount: Number(product?.soldCount ?? 0),
  };
}

/** 创建默认编辑表单。 */
export function createDefaultFlashSaleEditorForm(): FlashSaleEditorForm {
  return {
    id: '',
    name: '',
    cycleType: 'once',
    recurringDateMode: 'fixed',
    validDateRange: null,
    timeRange: null,
    weekDays: [4],
    channels: ['delivery', 'pickup'],
    perUserLimit: null,
    products: [],
    metrics: createEmptyFlashSaleMetrics(),
    status: 'active',
    storeIds: [],
  };
}
