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

export interface ScalperCfg { symbol:string; clipSz:number; triggerSpreadBps:number; tpBps:number; slBps:number; cooldownMs:number; }
export class MicroScalper {
  private last=0;
  private readonly logger: pino.Logger;
  constructor(private cfg:ScalperCfg, private router:OrderRouter, private getBook:()=>OrderBook|undefined, logger: pino.Logger){
    this.logger = logger.child({ name: "MicroScalper" });
  }
  async onBook(){
    const now=Date.now(); if (now-this.last<this.cfg.cooldownMs) return;
    const b=this.getBook(); if(!b?.bids?.[0]||!b?.asks?.[0]) return;
    const mid=(b.bids[0].px+b.asks[0].px)/2;
    const spread=((b.asks[0].px-b.bids[0].px)/mid)*1e4;
    if (spread>this.cfg.triggerSpreadBps){
      this.last=now;
      await this.router.sendLimitChild({ clientId:`scalp-${now}`, symbol:this.cfg.symbol, side:"buy", px:b.bids[0].px, sz:this.cfg.clipSz, tif:"IOC" });
      // For brevity, OCO/take-profit is handled by TriggerEngine (not shown).
    }
  }

  async onFill(fill: Fill): Promise<void> {
    this.logger.info({ fill }, "MicroScalper fill processed");
  }
}
