import {
  PipelineRunner,
  StateManager,
  createInteractionToolsFromDeps,
  type InteractionRuntimeTools,
} from "@actalk/inkos-core";
import { buildPipelineConfig, loadConfig } from "../utils.js";

type CliPipelineLike = Pick<PipelineRunner, "writeNextChapter" | "reviseDraft">;
type CliStateLike = Pick<StateManager, "ensureControlDocuments" | "bookDir" | "loadBookConfig" | "loadChapterIndex" | "saveChapterIndex" | "listBooks">;
type CliInteractionToolHooks = {
  readonly onChatTextDelta?: (text: string) => void;
  readonly onDraftTextDelta?: (text: string) => void;
  readonly getChatRequestOptions?: () => {
    readonly temperature?: number;
    readonly maxTokens?: number;
  };
};

export function createCliInteractionToolsFromDeps(
  pipeline: CliPipelineLike,
  state: CliStateLike,
  hooks?: CliInteractionToolHooks,
): InteractionRuntimeTools {
  return createInteractionToolsFromDeps(pipeline, state, hooks);
}

// Backward-compatible export for existing CLI interaction tests.
export function createInteractionToolsFromDepsCompat(
  _projectRoot: string,
  pipeline: CliPipelineLike,
  state: CliStateLike,
  hooks?: CliInteractionToolHooks,
): InteractionRuntimeTools {
  return createInteractionToolsFromDeps(pipeline, state, hooks);
}

export { createInteractionToolsFromDepsCompat as createInteractionToolsFromDeps };

export async function createInteractionTools(
  projectRoot: string,
  hooks?: CliInteractionToolHooks,
  options?: { readonly requireApiKey?: boolean },
): Promise<InteractionRuntimeTools> {
  const config = await loadConfig({ projectRoot, requireApiKey: options?.requireApiKey });
  const pipeline = new PipelineRunner(buildPipelineConfig(config, projectRoot));
  const state = new StateManager(projectRoot);
  return createInteractionToolsFromDeps(pipeline, state, hooks);
}
