import { TeamStatistics } from '../aggregates';

export interface LeaderboardEntry {
  userId: bigint;
  score: number;
  rank: number;
  totalTeamCount: number;
  directReferralCount: number;
}

export interface LeaderboardQueryOptions {
  limit?: number;
  offset?: number;
}

/**
 * 团队统计仓储接口
 */
export interface ITeamStatisticsRepository {
  /**
   * 保存团队统计
   */
  save(statistics: TeamStatistics): Promise<TeamStatistics>;

  /**
   * 根据用户ID查找
   */
  findByUserId(userId: bigint): Promise<TeamStatistics | null>;

  /**
   * 批量获取用户的团队统计
   */
  findByUserIds(userIds: bigint[]): Promise<TeamStatistics[]>;

  /**
   * 获取龙虎榜排名
   */
  getLeaderboard(options?: LeaderboardQueryOptions): Promise<LeaderboardEntry[]>;

  /**
   * 获取用户在龙虎榜的排名
   */
  getUserRank(userId: bigint): Promise<number | null>;

  /**
   * 批量更新团队统计 (用于认种事件处理)
   */
  batchUpdateTeamCounts(
    updates: Array<{
      userId: bigint;
      countDelta: number;
      provinceCode: string;
      cityCode: string;
      fromDirectReferralId?: bigint;
    }>,
  ): Promise<void>;

  /**
   * [纯新增] 批量更新团队统计（转让场景，支持负数 delta）
   * 与 batchUpdateTeamCounts 区别：负数 delta 时重新扫描所有直推找最大支线
   */
  batchUpdateTeamCountsForTransfer(
    updates: Array<{
      userId: bigint;
      countDelta: number;
      provinceCode: string;
      cityCode: string;
      fromDirectReferralId?: bigint;
    }>,
  ): Promise<void>;

  /**
   * 创建初始团队统计记录
   */
  create(userId: bigint, accountSequence: string): Promise<TeamStatistics>;
}

export const TEAM_STATISTICS_REPOSITORY = Symbol('ITeamStatisticsRepository');
