import type { Dayjs } from 'dayjs';

import type {
  MarketingPunchCardDetailDto,
  MarketingPunchCardExpireStrategy,
  MarketingPunchCardListItemDto,
  MarketingPunchCardScopeType,
  MarketingPunchCardStatsDto,
  MarketingPunchCardStatus,
  MarketingPunchCardTemplateOptionDto,
  MarketingPunchCardUsageMode,
  MarketingPunchCardUsageRecordDto,
  MarketingPunchCardUsageStatsDto,
  MarketingPunchCardValidityType,
} from '#/api/marketing';
import type { ProductCategoryDto, ProductPickerItemDto } from '#/api/product';

/**
 * 文件职责：次卡管理页面类型定义。
 */

/** 页面分段。 */
export type PunchCardTabKey = 'list' | 'records';

/** 次卡列表筛选表单。 */
export interface PunchCardListFilterForm {
  status: '' | MarketingPunchCardStatus;
}

/** 使用记录筛选表单。 */
export interface PunchCardUsageFilterForm {
  status: '' | 'expired' | 'normal' | 'used_up';
  templateId: string;
}

/** 次卡适用范围表单。 */
export interface PunchCardScopeForm {
  categoryIds: string[];
  productIds: string[];
  scopeType: MarketingPunchCardScopeType;
  tagIds: string[];
}

/** 次卡编辑抽屉表单。 */
export interface PunchCardEditorForm {
  allowTransfer: boolean;
  coverImageUrl: string;
  dailyLimit: null | number;
  description: string;
  expireStrategy: MarketingPunchCardExpireStrategy;
  id: string;
  name: string;
  notifyChannels: string[];
  originalPrice: null | number;
  perOrderLimit: null | number;
  perUserPurchaseLimit: null | number;
  salePrice: null | number;
  scope: PunchCardScopeForm;
  status: MarketingPunchCardStatus;
  totalTimes: null | number;
  usageCapAmount: null | number;
  usageMode: MarketingPunchCardUsageMode;
  validityDays: null | number;
  validityType: MarketingPunchCardValidityType;
  validDateRange: [Dayjs, Dayjs] | null;
}

/** 次卡卡片视图模型。 */
export type PunchCardTemplateCardViewModel = MarketingPunchCardListItemDto;

/** 次卡统计视图模型。 */
export type PunchCardStatsViewModel = MarketingPunchCardStatsDto;

/** 使用记录项视图模型。 */
export interface PunchCardUsageRecordViewModel extends MarketingPunchCardUsageRecordDto {
  displayStatusText: string;
}

/** 使用记录统计视图模型。 */
export type PunchCardUsageStatsViewModel = MarketingPunchCardUsageStatsDto;

/** 使用记录分页模型。 */
export interface PunchCardUsagePager {
  items: PunchCardUsageRecordViewModel[];
  page: number;
  pageSize: number;
  totalCount: number;
}

/** 次卡下拉选项。 */
export type PunchCardTemplateOptionViewModel =
  MarketingPunchCardTemplateOptionDto;

/** 二级抽屉分类项。 */
export type PunchCardPickerCategoryItem = ProductCategoryDto;

/** 二级抽屉商品项。 */
export type PunchCardPickerProductItem = ProductPickerItemDto;

/** 抽屉模式。 */
export type PunchCardDrawerMode = 'create' | 'edit';

/** 创建默认使用记录分页。 */
export function createDefaultPunchCardUsagePager(): PunchCardUsagePager {
  return {
    items: [],
    page: 1,
    pageSize: 10,
    totalCount: 0,
  };
}

/** 标准化详情转表单。 */
export function createEditorFormByDetail(
  detail: MarketingPunchCardDetailDto,
  validDateRange: [Dayjs, Dayjs] | null,
): PunchCardEditorForm {
  return {
    id: detail.id,
    name: detail.name,
    coverImageUrl: detail.coverImageUrl ?? '',
    salePrice: detail.salePrice,
    originalPrice: detail.originalPrice,
    totalTimes: detail.totalTimes,
    validityType: detail.validityType,
    validityDays: detail.validityDays,
    validDateRange,
    scope: {
      scopeType: detail.scope.scopeType,
      categoryIds: [...detail.scope.categoryIds],
      tagIds: [...detail.scope.tagIds],
      productIds: [...detail.scope.productIds],
    },
    usageMode: detail.usageMode,
    usageCapAmount: detail.usageCapAmount,
    dailyLimit: detail.dailyLimit,
    perOrderLimit: detail.perOrderLimit,
    perUserPurchaseLimit: detail.perUserPurchaseLimit,
    allowTransfer: detail.allowTransfer,
    expireStrategy: detail.expireStrategy,
    description: detail.description ?? '',
    notifyChannels: [...detail.notifyChannels],
    status: detail.status,
  };
}
