import { AggregateRoot } from '../aggregate-root.base';
import { ChainType, EvmAddress } from '@/domain/value-objects';

export interface MonitoredAddressProps {
  id?: bigint;
  chainType: ChainType;
  address: EvmAddress;
  accountSequence: string; // 跨服务关联标识 (格式: D + YYMMDD + 5位序号)
  userId: bigint; // 保留兼容
  isActive: boolean;
  createdAt?: Date;
  updatedAt?: Date;
}

export class MonitoredAddress extends AggregateRoot<bigint> {
  private props: MonitoredAddressProps;

  private constructor(props: MonitoredAddressProps) {
    super();
    this.props = props;
  }

  // Getters
  get id(): bigint | undefined {
    return this.props.id;
  }
  get chainType(): ChainType {
    return this.props.chainType;
  }
  get address(): EvmAddress {
    return this.props.address;
  }
  get accountSequence(): string {
    return this.props.accountSequence;
  }
  get userId(): bigint {
    return this.props.userId;
  }
  get isActive(): boolean {
    return this.props.isActive;
  }
  get createdAt(): Date | undefined {
    return this.props.createdAt;
  }
  get updatedAt(): Date | undefined {
    return this.props.updatedAt;
  }

  /**
   * 创建新的监控地址
   */
  static create(params: {
    chainType: ChainType;
    address: EvmAddress;
    accountSequence: string;
    userId: bigint;
  }): MonitoredAddress {
    return new MonitoredAddress({
      ...params,
      isActive: true,
    });
  }

  /**
   * 从持久化数据重建
   */
  static reconstitute(props: MonitoredAddressProps): MonitoredAddress {
    return new MonitoredAddress(props);
  }

  /**
   * 激活地址监控
   */
  activate(): void {
    this.props.isActive = true;
  }

  /**
   * 停用地址监控
   */
  deactivate(): void {
    this.props.isActive = false;
  }
}
