/**
 * 用户快照值对象
 *
 * 存储排名时刻的用户信息快照
 * 用于显示历史排名数据时保持一致性
 */
export class UserSnapshot {
  private constructor(
    public readonly userId: bigint,
    public readonly nickname: string,
    public readonly avatar: string | null,
    public readonly accountNo: string | null,
  ) {}

  /**
   * 创建用户快照
   */
  static create(params: {
    userId: bigint;
    nickname: string;
    avatar?: string | null;
    accountNo?: string | null;
  }): UserSnapshot {
    return new UserSnapshot(
      params.userId,
      params.nickname,
      params.avatar || null,
      params.accountNo || null,
    );
  }

  /**
   * 从JSON数据重建
   */
  static fromJson(json: Record<string, any>): UserSnapshot {
    return new UserSnapshot(
      BigInt(json.userId),
      json.nickname,
      json.avatar || null,
      json.accountNo || null,
    );
  }

  /**
   * 转换为JSON对象
   */
  toJson(): Record<string, any> {
    return {
      userId: this.userId.toString(),
      nickname: this.nickname,
      avatar: this.avatar,
      accountNo: this.accountNo,
    };
  }

  /**
   * 获取脱敏后的昵称（用于公开展示）
   */
  getMaskedNickname(): string {
    if (this.nickname.length <= 2) {
      return this.nickname[0] + '*';
    }
    const firstChar = this.nickname[0];
    const lastChar = this.nickname[this.nickname.length - 1];
    return `${firstChar}${'*'.repeat(this.nickname.length - 2)}${lastChar}`;
  }

  /**
   * 获取默认头像URL
   */
  getAvatarOrDefault(): string {
    return this.avatar || `https://api.dicebear.com/7.x/avataaars/svg?seed=${this.userId}`;
  }

  equals(other: UserSnapshot): boolean {
    return this.userId === other.userId;
  }

  toString(): string {
    return `UserSnapshot(${this.userId}: ${this.nickname})`;
  }
}
