import { ChainType, TxHash, BlockNumber } from '@/domain/value-objects';
import { MarketMakerAssetType, MarketMakerDepositStatus } from '@/domain/enums';

export const MARKET_MAKER_DEPOSIT_REPOSITORY = Symbol('MARKET_MAKER_DEPOSIT_REPOSITORY');

/**
 * 做市商充值记录 DTO
 */
export interface MarketMakerDepositDto {
  id?: bigint;
  chainType: string;
  txHash: string;
  fromAddress: string;
  toAddress: string;
  assetType: MarketMakerAssetType;
  tokenContract: string;
  amount: bigint;
  amountFormatted: string;
  blockNumber: bigint;
  blockTimestamp: Date;
  logIndex: number;
  confirmations: number;
  status: MarketMakerDepositStatus;
  notifiedAt?: Date | null;
  notifyAttempts: number;
  lastNotifyError?: string | null;
  creditedAt?: Date | null;
  creditReference?: string | null;
  createdAt?: Date;
  updatedAt?: Date;
}

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

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

  /**
   * 根据交易哈希查找
   */
  findByTxHash(txHash: string): Promise<MarketMakerDepositDto | null>;

  /**
   * 检查交易是否存在
   */
  existsByTxHash(txHash: string): Promise<boolean>;

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

  /**
   * 查找待通知的充值
   */
  findPendingNotification(): Promise<MarketMakerDepositDto[]>;

  /**
   * 根据资产类型查找
   */
  findByAssetType(
    chainType: ChainType,
    assetType: MarketMakerAssetType,
    limit?: number,
  ): Promise<MarketMakerDepositDto[]>;

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

  /**
   * 更新通知状态
   */
  updateNotifyStatus(
    id: bigint,
    notifiedAt: Date,
    notifyAttempts: number,
    error?: string,
  ): Promise<void>;

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

export const MARKET_MAKER_CHECKPOINT_REPOSITORY = Symbol('MARKET_MAKER_CHECKPOINT_REPOSITORY');

export interface MarketMakerCheckpointDto {
  id?: bigint;
  chainType: string;
  assetType: MarketMakerAssetType;
  lastScannedBlock: bigint;
  lastScannedAt: Date;
  isHealthy: boolean;
  lastError?: string | null;
}

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

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

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

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

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