import type {
  MarketingPunchCardExpireStrategy,
  MarketingPunchCardScopeType,
  MarketingPunchCardStatus,
  MarketingPunchCardUsageFilterStatus,
  MarketingPunchCardUsageMode,
  MarketingPunchCardValidityType,
} from '#/api/marketing';
import type {
  PunchCardEditorForm,
  PunchCardListFilterForm,
  PunchCardScopeForm,
  PunchCardUsageFilterForm,
  PunchCardUsageStatsViewModel,
} from '#/views/marketing/punch-card/types';

/**
 * 文件职责：次卡页面常量与默认值。
 */

/** 查看权限码。 */
export const PUNCH_CARD_VIEW_PERMISSION = 'tenant:marketing:punch-card:view';

/** 管理权限码。 */
export const PUNCH_CARD_MANAGE_PERMISSION =
  'tenant:marketing:punch-card:manage';

/** 列表状态筛选项。 */
export const PUNCH_CARD_STATUS_FILTER_OPTIONS: Array<{
  label: string;
  value: '' | MarketingPunchCardStatus;
}> = [
  { label: '全部状态', value: '' },
  { label: '上架', value: 'enabled' },
  { label: '下架', value: 'disabled' },
];

/** 使用记录状态筛选项。 */
export const PUNCH_CARD_RECORD_STATUS_FILTER_OPTIONS: Array<{
  label: string;
  value: '' | MarketingPunchCardUsageFilterStatus;
}> = [
  { label: '全部状态', value: '' },
  { label: '正常使用', value: 'normal' },
  { label: '已用完', value: 'used_up' },
  { label: '已过期', value: 'expired' },
];

/** 有效期类型选项。 */
export const PUNCH_CARD_VALIDITY_OPTIONS: Array<{
  label: string;
  value: MarketingPunchCardValidityType;
}> = [
  { label: '固定天数', value: 'days' },
  { label: '日期范围', value: 'range' },
];

/** 适用范围选项。 */
export const PUNCH_CARD_SCOPE_OPTIONS: Array<{
  label: string;
  value: MarketingPunchCardScopeType;
}> = [
  { label: '全部商品', value: 'all' },
  { label: '指定分类', value: 'category' },
  { label: '指定标签', value: 'tag' },
  { label: '指定商品', value: 'product' },
];

/** 使用模式选项。 */
export const PUNCH_CARD_USAGE_MODE_OPTIONS: Array<{
  label: string;
  value: MarketingPunchCardUsageMode;
}> = [
  { label: '完全免费', value: 'free' },
  { label: '金额上限', value: 'cap' },
];

/** 过期策略选项。 */
export const PUNCH_CARD_EXPIRE_STRATEGY_OPTIONS: Array<{
  label: string;
  value: MarketingPunchCardExpireStrategy;
}> = [
  { label: '剩余次数作废', value: 'invalidate' },
  { label: '可申请退款', value: 'refund' },
];

/** 通知渠道选项。 */
export const PUNCH_CARD_NOTIFY_CHANNEL_OPTIONS: Array<{
  label: string;
  value: string;
}> = [
  { label: '站内消息', value: 'in_app' },
  { label: '短信通知', value: 'sms' },
];

/** 适用范围文案。 */
export const PUNCH_CARD_SCOPE_TEXT_MAP: Record<
  MarketingPunchCardScopeType,
  string
> = {
  all: '全部商品',
  category: '指定分类',
  tag: '指定标签',
  product: '指定商品',
};

/** 状态文案。 */
export const PUNCH_CARD_STATUS_TEXT_MAP: Record<
  MarketingPunchCardStatus,
  string
> = {
  enabled: '上架',
  disabled: '下架',
};

/** 记录状态文案。 */
export const PUNCH_CARD_RECORD_STATUS_TEXT_MAP: Record<string, string> = {
  normal: '正常使用',
  almost_used_up: '即将用完',
  used_up: '已用完',
  expired: '已过期',
};

/** 创建默认列表筛选。 */
export function createDefaultPunchCardListFilterForm(): PunchCardListFilterForm {
  return {
    status: '',
  };
}

/** 创建默认使用记录筛选。 */
export function createDefaultPunchCardUsageFilterForm(): PunchCardUsageFilterForm {
  return {
    templateId: '',
    status: '',
  };
}

/** 创建默认适用范围。 */
export function createDefaultPunchCardScopeForm(
  scopeType: MarketingPunchCardScopeType = 'all',
): PunchCardScopeForm {
  return {
    scopeType,
    categoryIds: [],
    tagIds: [],
    productIds: [],
  };
}

/** 创建默认编辑表单。 */
export function createDefaultPunchCardEditorForm(): PunchCardEditorForm {
  return {
    id: '',
    name: '',
    coverImageUrl: '',
    salePrice: null,
    originalPrice: null,
    totalTimes: null,
    validityType: 'days',
    validityDays: 30,
    validDateRange: null,
    scope: createDefaultPunchCardScopeForm('all'),
    usageMode: 'free',
    usageCapAmount: null,
    dailyLimit: 1,
    perOrderLimit: 1,
    perUserPurchaseLimit: null,
    allowTransfer: false,
    expireStrategy: 'invalidate',
    description: '',
    notifyChannels: ['in_app'],
    status: 'enabled',
  };
}

/** 创建默认记录统计。 */
export function createDefaultPunchCardUsageStats(): PunchCardUsageStatsViewModel {
  return {
    todayUsedCount: 0,
    monthUsedCount: 0,
    expiringSoonCount: 0,
  };
}
