import { Decimal } from '@prisma/client/runtime/library';

/**
 * 活跃度等级
 */
export enum ActiveLevel {
  HIGH = '高活跃',
  MEDIUM = '中活跃',
  LOW = '低活跃',
  SILENT = '沉默',
}

/**
 * 价值等级
 */
export enum ValueLevel {
  HIGH = '高价值',
  MEDIUM = '中价值',
  LOW = '低价值',
  POTENTIAL = '潜力',
}

/**
 * 生命周期阶段
 */
export enum LifecycleStage {
  NEW = '新用户',
  GROWTH = '成长期',
  MATURE = '成熟期',
  DECLINE = '衰退期',
  CHURNED = '流失',
}

/**
 * 用户特征实体 - 计算后的用户画像指标
 */
export class UserFeatureEntity {
  constructor(
    public readonly id: string,
    public readonly accountSequence: string,
    // RFM 模型
    public readonly rfmRecency: number | null,
    public readonly rfmFrequency: number | null,
    public readonly rfmMonetary: Decimal | null,
    public readonly rfmScore: number | null,
    // 活跃度
    public readonly activeLevel: string | null,
    public readonly lastActiveAt: Date | null,
    // 价值分层
    public readonly valueLevel: string | null,
    public readonly lifetimeValue: Decimal | null,
    // 生命周期
    public readonly lifecycleStage: string | null,
    // 自定义特征
    public readonly customFeatures: Record<string, unknown> | null,
    public readonly createdAt: Date,
    public readonly updatedAt: Date,
  ) {}

  /**
   * 创建用户特征
   */
  static create(params: {
    id: string;
    accountSequence: string;
  }): UserFeatureEntity {
    const now = new Date();
    return new UserFeatureEntity(
      params.id,
      params.accountSequence,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      null,
      now,
      now,
    );
  }

  /**
   * 更新 RFM 指标
   */
  updateRfm(params: {
    recency?: number | null;
    frequency?: number | null;
    monetary?: Decimal | null;
    score?: number | null;
  }): UserFeatureEntity {
    return new UserFeatureEntity(
      this.id,
      this.accountSequence,
      params.recency !== undefined ? params.recency : this.rfmRecency,
      params.frequency !== undefined ? params.frequency : this.rfmFrequency,
      params.monetary !== undefined ? params.monetary : this.rfmMonetary,
      params.score !== undefined ? params.score : this.rfmScore,
      this.activeLevel,
      this.lastActiveAt,
      this.valueLevel,
      this.lifetimeValue,
      this.lifecycleStage,
      this.customFeatures,
      this.createdAt,
      new Date(),
    );
  }

  /**
   * 更新活跃度
   */
  updateActiveLevel(level: string, lastActiveAt?: Date): UserFeatureEntity {
    return new UserFeatureEntity(
      this.id,
      this.accountSequence,
      this.rfmRecency,
      this.rfmFrequency,
      this.rfmMonetary,
      this.rfmScore,
      level,
      lastActiveAt ?? this.lastActiveAt,
      this.valueLevel,
      this.lifetimeValue,
      this.lifecycleStage,
      this.customFeatures,
      this.createdAt,
      new Date(),
    );
  }

  /**
   * 更新价值分层
   */
  updateValueLevel(level: string, ltv?: Decimal): UserFeatureEntity {
    return new UserFeatureEntity(
      this.id,
      this.accountSequence,
      this.rfmRecency,
      this.rfmFrequency,
      this.rfmMonetary,
      this.rfmScore,
      this.activeLevel,
      this.lastActiveAt,
      level,
      ltv !== undefined ? ltv : this.lifetimeValue,
      this.lifecycleStage,
      this.customFeatures,
      this.createdAt,
      new Date(),
    );
  }

  /**
   * 更新生命周期阶段
   */
  updateLifecycleStage(stage: string): UserFeatureEntity {
    return new UserFeatureEntity(
      this.id,
      this.accountSequence,
      this.rfmRecency,
      this.rfmFrequency,
      this.rfmMonetary,
      this.rfmScore,
      this.activeLevel,
      this.lastActiveAt,
      this.valueLevel,
      this.lifetimeValue,
      stage,
      this.customFeatures,
      this.createdAt,
      new Date(),
    );
  }

  /**
   * 更新自定义特征
   */
  updateCustomFeatures(features: Record<string, unknown>): UserFeatureEntity {
    return new UserFeatureEntity(
      this.id,
      this.accountSequence,
      this.rfmRecency,
      this.rfmFrequency,
      this.rfmMonetary,
      this.rfmScore,
      this.activeLevel,
      this.lastActiveAt,
      this.valueLevel,
      this.lifetimeValue,
      this.lifecycleStage,
      { ...this.customFeatures, ...features },
      this.createdAt,
      new Date(),
    );
  }

  /**
   * 计算 RFM 综合分数 (0-100)
   * R: 最近活跃度 (越近越好)
   * F: 活跃频率 (越频繁越好)
   * M: 消费金额 (越多越好)
   */
  static calculateRfmScore(
    recency: number,
    frequency: number,
    monetary: number,
    weights = { r: 0.3, f: 0.3, m: 0.4 },
  ): number {
    // 归一化分数 (0-100)
    // Recency: 0-30天 = 100分, 30天以上递减
    const rScore = Math.max(0, Math.min(100, 100 - (recency / 30) * 100));

    // Frequency: 30天内活跃天数，30天 = 100分
    const fScore = Math.min(100, (frequency / 30) * 100);

    // Monetary: 根据业务定义，这里假设 10000 = 100分
    const mScore = Math.min(100, (monetary / 10000) * 100);

    return Math.round(rScore * weights.r + fScore * weights.f + mScore * weights.m);
  }
}
