import type { QueryExecutor } from '@traderalice/opentypebb'
import type { UTAManagerSDK } from '../services/uta-client/index.js'
import type { INewsProvider } from '../domain/news/types.js'
import type { MarketSearchDeps } from '../domain/market-data/aggregate-search.js'
import type { CronEngine } from '../task/cron/engine.js'
import type { Heartbeat } from '../task/heartbeat/index.js'
import type { Config, WebChannel } from './config.js'
import type { GenerateRouter } from './ai-provider-manager.js'
import type { EventLog } from './event-log.js'
import type { ToolCallLog } from './tool-call-log.js'
import type { ToolCenter } from './tool-center.js'
import type { ListenerRegistry } from './listener-registry.js'
import type { EventBus } from './event-bus.js'
import type { IInboxStore } from './inbox-store.js'
import type { IEntityStore } from './entity-store.js'

export type { Config, WebChannel }

export interface Plugin {
  name: string
  start(ctx: EngineContext): Promise<void>
  stop(): Promise<void>
}

/** Generic result of an out-of-band reconnect attempt. Still used by the
 *  UTA client SDK (`reconnectUTA`); the connector-reconnect path that
 *  used to share it was removed with the legacy connector cluster. */
export interface ReconnectResult {
  success: boolean
  error?: string
  message?: string
}

export interface EngineContext {
  config: Config
  /** Workspace-anchored push surface (Linear-inbox style). Written by
   *  workspace agents (via the inbox_push MCP tool) and by AgentWork's
   *  autonomous trigger sources (cron / task), which append directly
   *  under a synthetic `automation:<source>` workspace id. */
  inboxStore: IInboxStore
  /** Durable cross-workspace tracked-index (assets / topics). Written by
   *  workspace agents via the entity_upsert MCP tool; read by the Tracked
   *  tab. Notes point at entities with `[[name]]` links. */
  entityStore: IEntityStore
  /** Provider router — resolves the active profile to an AIProvider and
   *  drives one-shot / test calls. The in-process agent loop (formerly
   *  AgentCenter) is retired; the model loop now runs inside the native
   *  workspace CLIs, and AgentWork drives this router directly. */
  router: GenerateRouter
  eventLog: EventLog
  toolCallLog: ToolCallLog
  heartbeat: Heartbeat
  cronEngine: CronEngine
  toolCenter: ToolCenter
  listenerRegistry: ListenerRegistry
  /** Ergonomic in-process producer facade. Use this to fire events from
   *  plugins / hacks / extension code instead of plumbing eventLog. */
  fire: EventBus

  // Market data
  bbEngine: QueryExecutor
  /** Deps for cross-asset-class heuristic symbol search. Shared between the
   *  AI tool (marketSearchForResearch) and the /api/market/search HTTP route. */
  marketSearch: MarketSearchDeps

  // Trading — HTTP-backed SDK that talks to the co-located UTA service.
  // FxService and SnapshotService live entirely inside UTA after Step 6;
  // anything Alice used to read off `ctx.fxService` / `ctx.snapshotService`
  // now goes through the SDK (e.g. `await utaManager.getAggregatedEquity()`
  // for FX-converted totals).
  utaManager: UTAManagerSDK
  newsProvider?: INewsProvider
}

/** A media attachment collected from tool results (e.g. browser screenshots). */
export interface MediaAttachment {
  type: 'image'
  /** Absolute path to the file on disk. */
  path: string
}
