import { ChainType, BlockNumber } from '@/domain/value-objects';
import { PoolAccountType, PoolAccountDepositStatus } from '@/domain/enums';

export const POOL_ACCOUNT_DEPOSIT_REPOSITORY = Symbol('POOL_ACCOUNT_DEPOSIT_REPOSITORY');

/**
 * 池账户充值记录 DTO
 */
export interface PoolAccountDepositDto {
  id?: bigint;
  chainType: string;
  txHash: string;
  fromAddress: string;
  toAddress: string;
  poolType: PoolAccountType;
  tokenContract: string;
  amount: bigint;
  amountFormatted: string;
  blockNumber: bigint;
  blockTimestamp: Date;
  logIndex: number;
  confirmations: number;
  status: PoolAccountDepositStatus;
  creditedAt?: Date | null;
  creditReference?: string | null;
  createdAt?: Date;
  updatedAt?: Date;
}

export interface IPoolAccountDepositRepository {
  /**
   * 保存充值记录
   */
  save(deposit: PoolAccountDepositDto): Promise<PoolAccountDepositDto>;

  /**
   * 根据ID查找
   */
  findById(id: bigint): Promise<PoolAccountDepositDto | null>;

  /**
   * 检查交易是否存在（按txHash+logIndex）
   */
  existsByTxHashAndLogIndex(txHash: string, logIndex: number): Promise<boolean>;

  /**
   * 查找待确认的充值
   */
  findPendingConfirmation(chainType: ChainType): Promise<PoolAccountDepositDto[]>;

  /**
   * 更新确认状态
   */
  updateConfirmations(
    id: bigint,
    confirmations: number,
    status: PoolAccountDepositStatus,
  ): Promise<void>;

  /**
   * 更新入账状态
   */
  updateCreditedStatus(
    id: bigint,
    creditedAt: Date,
    creditReference: string,
  ): Promise<void>;
}

export const POOL_ACCOUNT_CHECKPOINT_REPOSITORY = Symbol('POOL_ACCOUNT_CHECKPOINT_REPOSITORY');

export interface IPoolAccountCheckpointRepository {
  /**
   * 获取上次扫描的区块
   */
  getLastScannedBlock(chainType: ChainType, poolType: PoolAccountType): Promise<BlockNumber | null>;

  /**
   * 更新检查点
   */
  updateCheckpoint(chainType: ChainType, poolType: PoolAccountType, blockNumber: BlockNumber): Promise<void>;

  /**
   * 初始化检查点（如果不存在）
   */
  initializeIfNotExists(chainType: ChainType, poolType: PoolAccountType, blockNumber: BlockNumber): Promise<void>;

  /**
   * 记录错误
   */
  recordError(chainType: ChainType, poolType: PoolAccountType, error: string): Promise<void>;

  /**
   * 标记健康
   */
  markHealthy(chainType: ChainType, poolType: PoolAccountType): Promise<void>;
}
