import type {
  MarketingSeckillActivityType,
  MarketingSeckillChannel,
  MarketingSeckillDisplayStatus,
  MarketingSeckillProductStatus,
} from '#/api/marketing';
import type {
  SeckillEditorForm,
  SeckillEditorProductForm,
  SeckillEditorSessionForm,
  SeckillFilterForm,
} from '#/views/marketing/seckill/types';

/**
 * 文件职责：秒杀活动页面常量与默认表单。
 */

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

/** 活动类型选项。 */
export const SECKILL_ACTIVITY_TYPE_OPTIONS: Array<{
  label: string;
  value: MarketingSeckillActivityType;
}> = [
  { label: '限时秒杀', value: 'timed' },
  { label: '整点秒杀', value: 'hourly' },
];

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

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

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

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

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

/** 创建空指标对象。 */
export function createEmptySeckillMetrics() {
  return {
    participantCount: 0,
    dealCount: 0,
    conversionRate: 0,
    monthlySeckillSalesCount: 0,
  };
}

/** 创建默认场次表单项。 */
export function createDefaultSeckillSessionForm(
  session?: Partial<SeckillEditorSessionForm>,
): SeckillEditorSessionForm {
  return {
    key: session?.key ?? `${Date.now()}-${Math.random()}`,
    startTime: session?.startTime ?? '',
    durationMinutes:
      session?.durationMinutes === null ||
      session?.durationMinutes === undefined
        ? 60
        : Number(session.durationMinutes),
  };
}

/** 创建默认商品表单。 */
export function createDefaultSeckillProductForm(
  product?: Partial<SeckillEditorProductForm>,
): SeckillEditorProductForm {
  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),
    seckillPrice:
      product?.seckillPrice === null || product?.seckillPrice === undefined
        ? null
        : Number(product.seckillPrice),
    stockLimit:
      product?.stockLimit === null || product?.stockLimit === undefined
        ? null
        : Number(product.stockLimit),
    perUserLimit:
      product?.perUserLimit === null || product?.perUserLimit === undefined
        ? null
        : Number(product.perUserLimit),
    soldCount: Number(product?.soldCount ?? 0),
  };
}

/** 创建默认编辑表单。 */
export function createDefaultSeckillEditorForm(): SeckillEditorForm {
  return {
    id: '',
    name: '',
    activityType: 'timed',
    validDateRange: null,
    timeRange: null,
    sessions: [
      createDefaultSeckillSessionForm({
        startTime: '10:00',
        durationMinutes: 60,
      }),
      createDefaultSeckillSessionForm({
        startTime: '14:00',
        durationMinutes: 60,
      }),
      createDefaultSeckillSessionForm({
        startTime: '18:00',
        durationMinutes: 60,
      }),
    ],
    channels: ['delivery', 'pickup', 'dine_in'],
    perUserLimit: null,
    preheatEnabled: true,
    preheatHours: 2,
    products: [],
    metrics: createEmptySeckillMetrics(),
    status: 'active',
    storeIds: [],
  };
}
