/**
 * 活动模型
 */

// 活动类型（前端语义化）
export type ActivityType = 'discount' | 'full_reduction' | 'seckill' | 'group_buy' | 'flash_sale'

// 活动状态（前端语义化）
export type ActivityStatus = 'upcoming' | 'ongoing' | 'ended' | 'cancelled'

// 活动数据（与后端字段对齐）
export interface ShopActivity {
  id: number
  name: string
  image: string
  banner?: string
  startTime: string
  endTime: string
  /** 后端返回整数: 0=未开始 1=进行中 2=已结束，前端通过 normalizeActivity 转为语义化字符串 */
  status: ActivityStatus | number
  /** 后端返回整数: 1=满减 2=折扣 3=秒杀 4=拼团 */
  type: ActivityType | number
  typeName?: string
  description: string
  participants?: number
  maxParticipants?: number
  discountRate?: number
  reductionAmount?: number
  minAmount?: number
  rules?: string
  tenantId?: number
  createTime: string
  updateTime?: string
}

// 活动查询参数
export interface ShopActivityParam {
  page?: number
  limit?: number
  status?: number | ActivityStatus
  type?: number | ActivityType
  keywords?: string
}

// 活动报名参数
export interface ActivitySignUpParams {
  activityId: number
  userId?: number
  contactPhone?: string
  remark?: string
}

// 活动统计
export interface ActivityStats {
  total: number
  ongoing: number
  upcoming: number
  ended: number
  totalParticipants: number
}

// 后端整数状态 → 前端语义化状态
export function normalizeActivityStatus(status: number | ActivityStatus): ActivityStatus {
  if (typeof status === 'string') return status
  const map: Record<number, ActivityStatus> = { 0: 'upcoming', 1: 'ongoing', 2: 'ended' }
  return map[status] ?? 'ended'
}

// 后端整数类型 → 前端语义化类型
export function normalizeActivityType(type: number | ActivityType): ActivityType {
  if (typeof type === 'string') return type
  const map: Record<number, ActivityType> = {
    1: 'full_reduction',
    2: 'discount',
    3: 'seckill',
    4: 'group_buy',
  }
  return map[type] ?? 'discount'
}

// 规范化活动对象（处理后端整数字段）
export function normalizeActivity(a: ShopActivity): ShopActivity & { status: ActivityStatus; type: ActivityType } {
  return {
    ...a,
    participants: a.participants ?? 0,
    status: normalizeActivityStatus(a.status as number),
    type: normalizeActivityType(a.type as number),
  }
}
