import { UserAccount } from '@/domain/aggregates/user-account/user-account.aggregate';
import { WalletAddress } from '@/domain/entities/wallet-address.entity';
import {
  UserId,
  AccountSequence,
  PhoneNumber,
  ReferralCode,
  ChainType,
  AccountStatus,
  KYCStatus,
} from '@/domain/value-objects';

export interface Pagination {
  page: number;
  limit: number;
}

export interface ReferralLinkData {
  linkId: bigint;
  userId: bigint;
  referralCode: string;
  shortCode: string;
  channel: string | null;
  campaignId: string | null;
  createdAt: Date;
}

export interface CreateReferralLinkParams {
  userId: bigint;
  referralCode: string;
  shortCode: string;
  channel: string | null;
  campaignId: string | null;
}

export interface UserAccountRepository {
  save(account: UserAccount): Promise<void>;
  saveWallets(userId: UserId, wallets: WalletAddress[]): Promise<void>;
  findById(userId: UserId): Promise<UserAccount | null>;
  findByAccountSequence(sequence: AccountSequence): Promise<UserAccount | null>;
  findByDeviceId(deviceId: string): Promise<UserAccount | null>;
  findByPhoneNumber(phoneNumber: PhoneNumber): Promise<UserAccount | null>;
  findByReferralCode(referralCode: ReferralCode): Promise<UserAccount | null>;
  findByWalletAddress(
    chainType: ChainType,
    address: string,
  ): Promise<UserAccount | null>;
  getMaxAccountSequence(): Promise<AccountSequence | null>;
  getNextAccountSequence(): Promise<AccountSequence>;
  findUsers(
    filters?: {
      status?: AccountStatus;
      kycStatus?: KYCStatus;
      keyword?: string;
    },
    pagination?: Pagination,
  ): Promise<UserAccount[]>;
  countUsers(filters?: {
    status?: AccountStatus;
    kycStatus?: KYCStatus;
  }): Promise<number>;

  // 批量查询
  findByUserIds(userIds: UserId[]): Promise<UserAccount[]>;

  // 推荐相关
  findByInviterSequence(
    inviterSequence: AccountSequence,
  ): Promise<UserAccount[]>;
  createReferralLink(
    params: CreateReferralLinkParams,
  ): Promise<ReferralLinkData>;
  findReferralLinksByUserId(userId: UserId): Promise<ReferralLinkData[]>;

  // 钱包生成相关
  /**
   * 查找钱包地址不完整的用户（三链地址不全）
   *
   * 用于定时任务扫描需要重试钱包生成的用户
   * 返回所有三链地址不全的用户 ID 列表
   *
   * @param limit 最大返回数量，防止一次查询过多
   */
  findUsersWithIncompleteWallets(limit: number): Promise<UserId[]>;
}

export const USER_ACCOUNT_REPOSITORY = Symbol('USER_ACCOUNT_REPOSITORY');
