import Decimal from 'decimal.js';

/**
 * 同步的用户数据
 */
export interface SyncedUser {
  id: bigint;
  accountSequence: string;
  originalUserId: bigint;
  phone: string | null;
  status: string | null;
  sourceSequenceNum: bigint;
  syncedAt: Date;
  contributionCalculated: boolean;
  contributionCalculatedAt: Date | null;
  createdAt: Date;
}

/**
 * 同步的认种数据
 */
export interface SyncedAdoption {
  id: bigint;
  originalAdoptionId: bigint;
  accountSequence: string;
  treeCount: number;
  adoptionDate: Date;
  status: string | null;
  // 认种选择的省市（用于系统账户分配）
  selectedProvince: string | null;
  selectedCity: string | null;
  contributionPerTree: Decimal;
  sourceSequenceNum: bigint;
  syncedAt: Date;
  contributionDistributed: boolean;
  contributionDistributedAt: Date | null;
  createdAt: Date;
}

/**
 * 同步的推荐关系数据
 */
export interface SyncedReferral {
  id: bigint;
  accountSequence: string;
  referrerAccountSequence: string | null;
  referrerUserId: bigint | null; // 1.0 的 referrer_id
  originalUserId: bigint | null; // 1.0 的 user_id
  ancestorPath: string | null;
  depth: number;
  sourceSequenceNum: bigint;
  syncedAt: Date;
  createdAt: Date;
}

/**
 * 同步数据仓库接口
 */
export interface ISyncedDataRepository {
  // ============ 用户相关 ============

  /**
   * 保存或更新同步的用户数据
   */
  upsertSyncedUser(data: {
    accountSequence: string;
    originalUserId: bigint;
    phone?: string | null;
    status?: string | null;
    sourceSequenceNum: bigint;
  }): Promise<SyncedUser>;

  /**
   * 根据账户序列号查找用户
   */
  findSyncedUserByAccountSequence(accountSequence: string): Promise<SyncedUser | null>;

  /**
   * 获取未计算算力的用户
   */
  findUncalculatedUsers(limit?: number): Promise<SyncedUser[]>;

  /**
   * 标记用户算力已计算
   */
  markUserContributionCalculated(accountSequence: string, tx?: any): Promise<void>;

  // ============ 认种相关 ============

  /**
   * 保存或更新同步的认种数据
   */
  upsertSyncedAdoption(data: {
    originalAdoptionId: bigint;
    accountSequence: string;
    treeCount: number;
    adoptionDate: Date;
    status?: string | null;
    selectedProvince?: string | null;
    selectedCity?: string | null;
    contributionPerTree: Decimal;
    sourceSequenceNum: bigint;
  }): Promise<SyncedAdoption>;

  /**
   * 根据原始ID查找认种
   */
  findSyncedAdoptionByOriginalId(originalAdoptionId: bigint): Promise<SyncedAdoption | null>;

  /**
   * 获取未分配贡献值的认种
   */
  findUndistributedAdoptions(limit?: number): Promise<SyncedAdoption[]>;

  /**
   * 获取用户的所有认种
   */
  findAdoptionsByAccountSequence(accountSequence: string): Promise<SyncedAdoption[]>;

  /**
   * 标记认种贡献值已分配，同时保存分配摘要
   */
  markAdoptionContributionDistributed(
    originalAdoptionId: bigint,
    distributionSummary?: string,
    tx?: any,
  ): Promise<void>;

  /**
   * 获取账户的总认种棵数
   */
  getTotalTreesByAccountSequence(accountSequence: string): Promise<number>;

  // ============ 推荐关系相关 ============

  /**
   * 保存或更新同步的推荐关系
   */
  upsertSyncedReferral(data: {
    accountSequence: string;
    referrerAccountSequence?: string | null;
    referrerUserId?: bigint | null;
    originalUserId?: bigint | null;
    ancestorPath?: string | null;
    depth?: number;
    sourceSequenceNum: bigint;
  }): Promise<SyncedReferral>;

  /**
   * 根据原始用户ID查找推荐关系
   */
  findSyncedReferralByOriginalUserId(originalUserId: bigint): Promise<SyncedReferral | null>;

  /**
   * 根据账户序列号查找推荐关系
   */
  findSyncedReferralByAccountSequence(accountSequence: string): Promise<SyncedReferral | null>;

  /**
   * 获取直推列表（直接推荐的下级）
   */
  findDirectReferrals(referrerAccountSequence: string): Promise<SyncedReferral[]>;

  /**
   * 获取上线链条（最多15级）
   */
  findAncestorChain(accountSequence: string, maxLevel?: number): Promise<SyncedReferral[]>;

  /**
   * 获取直推中已认种的用户数
   */
  getDirectReferralAdoptedCount(referrerAccountSequence: string): Promise<number>;

  /**
   * 获取伞下各级的认种棵数
   */
  getTeamTreesByLevel(
    accountSequence: string,
    maxLevel?: number,
  ): Promise<Map<number, number>>;
}

export const SYNCED_DATA_REPOSITORY = Symbol('ISyncedDataRepository');
