/**
 * 用户查询仓储接口
 * 用于 admin-web 用户管理页面的查询操作
 */

export interface UserQueryItem {
  userId: bigint;
  accountSequence: string;
  nickname: string | null;
  avatarUrl: string | null;
  phoneNumberMasked: string | null;
  inviterSequence: string | null;
  kycStatus: string;
  personalAdoptionCount: number;
  teamAddressCount: number;
  teamAdoptionCount: number;
  provinceAdoptionCount: number;
  cityAdoptionCount: number;
  leaderboardRank: number | null;
  status: string;
  isOnline: boolean;
  registeredAt: Date;
  lastActiveAt: Date | null;
}

export interface UserQueryFilters {
  keyword?: string;           // 搜索关键词 (账号序列号/昵称)
  status?: string;            // 状态筛选
  kycStatus?: string;         // KYC状态筛选
  hasInviter?: boolean;       // 是否有推荐人
  minAdoptions?: number;      // 最小认种数
  maxAdoptions?: number;      // 最大认种数
  registeredAfter?: Date;     // 注册时间开始
  registeredBefore?: Date;    // 注册时间结束
}

export interface UserQuerySort {
  field: 'registeredAt' | 'personalAdoptionCount' | 'teamAdoptionCount' | 'leaderboardRank';
  order: 'asc' | 'desc';
}

export interface UserQueryPagination {
  page: number;
  pageSize: number;
}

export interface UserQueryResult {
  items: UserQueryItem[];
  total: number;
  page: number;
  pageSize: number;
  totalPages: number;
}

export const USER_QUERY_REPOSITORY = Symbol('USER_QUERY_REPOSITORY');

export interface IUserQueryRepository {
  /**
   * 查询用户列表
   */
  findMany(
    filters: UserQueryFilters,
    pagination: UserQueryPagination,
    sort?: UserQuerySort,
  ): Promise<UserQueryResult>;

  /**
   * 根据用户ID查询用户详情
   */
  findById(userId: bigint): Promise<UserQueryItem | null>;

  /**
   * 根据账号序列号查询用户详情
   */
  findByAccountSequence(accountSequence: string): Promise<UserQueryItem | null>;

  /**
   * 创建或更新用户视图记录 (用于 Kafka 事件同步)
   */
  upsert(data: {
    userId: bigint;
    accountSequence: string;
    nickname?: string | null;
    avatarUrl?: string | null;
    phoneNumberMasked?: string | null;
    inviterSequence?: string | null;
    kycStatus?: string;
    status?: string;
    registeredAt: Date;
  }): Promise<void>;

  /**
   * 更新用户资料
   */
  updateProfile(
    userId: bigint,
    data: {
      nickname?: string | null;
      avatarUrl?: string | null;
    },
  ): Promise<void>;

  /**
   * 更新认种统计
   */
  updateAdoptionStats(
    userId: bigint,
    data: {
      personalAdoptionCount?: number;
      teamAddressCount?: number;
      teamAdoptionCount?: number;
    },
  ): Promise<void>;

  /**
   * 更新授权统计
   */
  updateAuthorizationStats(
    userId: bigint,
    data: {
      provinceAdoptionCount?: number;
      cityAdoptionCount?: number;
    },
  ): Promise<void>;

  /**
   * 更新用户状态
   */
  updateStatus(userId: bigint, status: string): Promise<void>;

  /**
   * 更新 KYC 状态（按 userId）
   */
  updateKycStatus(userId: bigint, kycStatus: string): Promise<void>;

  /**
   * 更新 KYC 状态（按 accountSequence）
   */
  updateKycStatusByAccountSequence(accountSequence: string, kycStatus: string): Promise<void>;

  /**
   * 更新在线状态
   */
  updateOnlineStatus(userId: bigint, isOnline: boolean): Promise<void>;

  /**
   * 批量更新在线状态
   */
  batchUpdateOnlineStatus(userIds: bigint[], isOnline: boolean): Promise<void>;

  /**
   * 获取用户总数
   */
  count(filters?: UserQueryFilters): Promise<number>;

  /**
   * 检查用户是否存在（按 userId）
   */
  exists(userId: bigint): Promise<boolean>;

  /**
   * 检查用户是否存在（按 accountSequence）
   */
  existsByAccountSequence(accountSequence: string): Promise<boolean>;
}
