import { JsonRpcProvider } from 'ethers';
import { GenexConfig } from './types';
import { CouponModule } from './modules/coupon';
import { BlockModule } from './modules/blocks';
import { EventModule } from './modules/events';
import { SettlementModule } from './modules/settlement';

export class GenexClient {
  private provider: JsonRpcProvider;
  private config: Required<GenexConfig>;

  readonly coupon: CouponModule;
  readonly blocks: BlockModule;
  readonly events: EventModule;
  readonly settlement: SettlementModule;

  constructor(config: GenexConfig) {
    this.config = {
      rpcUrl: config.rpcUrl,
      chainId: config.chainId ?? 8888,
      wsUrl: config.wsUrl ?? config.rpcUrl.replace('http', 'ws'),
    };

    this.provider = new JsonRpcProvider(this.config.rpcUrl);
    this.coupon = new CouponModule(this.provider);
    this.blocks = new BlockModule(this.provider);
    this.events = new EventModule(this.config.wsUrl);
    this.settlement = new SettlementModule(this.provider);
  }

  getProvider(): JsonRpcProvider {
    return this.provider;
  }

  getChainId(): number {
    return this.config.chainId;
  }
}
