/**
 * installGhost — wires a recorder + player to the race events. Integration
 * calls this once, after the blob exists:
 *
 *   installGhost(world, blob)
 *
 * The recorder captures every run and persists the best; the player replays the
 * saved best on the next race. Both self-subscribe to race:started/finished.
 */
import type { Blob, World } from '../contracts'
import { GhostRecorder } from './recorder'
import { GhostPlayer, type GhostPlayerOptions } from './player'

export interface GhostHandles {
  recorder: GhostRecorder
  player: GhostPlayer
}

export function installGhost(
  world: World,
  blob: Blob,
  opts: GhostPlayerOptions = {},
): GhostHandles {
  const recorder = new GhostRecorder(world, blob)
  world.addSystem(recorder)

  const radius = opts.radius ?? (blob as { radius?: number }).radius ?? 0.5
  const player = new GhostPlayer(world, { ...opts, radius })

  return { recorder, player }
}
