import type { Fill, OrderBook } from "../../domain/src/types";
import { OrderRouter } from "../../execution/src/orderRouter";
import type pino from "pino";

export interface MMConfig { symbol:string; tickSz:number; clipSz:number; spreadBps:number; }
export class MarketMaker {
  private readonly logger: pino.Logger;

  constructor(private cfg:MMConfig, private router:OrderRouter, private getBook:()=>OrderBook|undefined, logger: pino.Logger){
    this.logger = logger.child({ name: "MarketMaker" });
  }
  async onTick(){
    const b = this.getBook(); if(!b?.bids?.[0] || !b?.asks?.[0]) return;
    const mid = (b.bids[0].px + b.asks[0].px)/2;
    const pxBid = Math.floor(mid*(1-this.cfg.spreadBps/1e4)/this.cfg.tickSz)*this.cfg.tickSz;
    const pxAsk = Math.ceil (mid*(1+this.cfg.spreadBps/1e4)/this.cfg.tickSz)*this.cfg.tickSz;
    const base = { symbol:this.cfg.symbol, tif:"GTC" as const, postOnly:true };
    await this.router.sendLimitChild({ ...base, clientId:`mm-bid-${Date.now()}`, side:"buy",  px:pxBid, sz:this.cfg.clipSz });
    await this.router.sendLimitChild({ ...base, clientId:`mm-ask-${Date.now()}`, side:"sell", px:pxAsk, sz:this.cfg.clipSz });
  }

  async onFill(fill: Fill): Promise<void> {
    this.logger.info({ fill }, "MarketMaker fill processed");
    // Hook for adapting inventory or re-quoting; current implementation simply logs.
  }
}
