import type { Config } from '../config/index.js';
import type { Lifecycle } from './lifecycle.js';
import type { AudioTranscriptionConfig } from '../models/media.js';
import { HookEngine } from '../hooks/index.js';
import { ToolRegistry, ToolExecutor, ToolPolicy, allBuiltinTools, createWebSearchTools, createProcessTools, ProcessManager, BrowserManager, createBrowserTools, createAudioTranscribeTool } from '../tools/index.js';

export interface ToolsDeps {
  config: Config;
  lifecycle: Lifecycle;
  hookEngine: HookEngine;
}

export interface ToolsResult {
  toolRegistry: ToolRegistry;
  toolExecutor: ToolExecutor;
  browserManager?: BrowserManager;
}

export function initTools(deps: ToolsDeps): ToolsResult {
  const { config, lifecycle, hookEngine } = deps;

  // Initialize tool registry and register all builtin tools
  const toolRegistry = new ToolRegistry();
  for (const tool of allBuiltinTools) {
    toolRegistry.register(tool);
  }

  // Register web search tool if configured with credentials
  if (config.web_search.api_key || config.web_search.endpoint) {
    for (const tool of createWebSearchTools({
      provider: config.web_search.provider,
      apiKey: config.web_search.api_key,
      endpoint: config.web_search.endpoint,
      maxResults: config.web_search.max_results,
    })) {
      toolRegistry.register(tool);
    }
  }

  // Initialize process manager and register process tools
  const processManager = new ProcessManager({
    maxConcurrent: config.process.max_concurrent,
    maxRuntimeMinutes: config.process.max_runtime_minutes,
    bufferSize: config.process.buffer_size,
  });

  for (const tool of createProcessTools(processManager)) {
    toolRegistry.register(tool);
  }

  lifecycle.onShutdown(async () => {
    await processManager.shutdown();
    console.log('Process manager stopped');
  });

  // Register audio transcription tool if configured
  if (config.audio?.enabled && config.audio.provider) {
    const audioConfig: AudioTranscriptionConfig = {
      endpoint: config.audio.provider.endpoint,
      apiKey: config.audio.provider.api_key,
      model: config.audio.provider.model,
    };
    const audioTool = createAudioTranscribeTool(audioConfig);
    toolRegistry.register(audioTool);
    console.log(`Audio transcription enabled (type=${config.audio.provider.type}, endpoint=${audioConfig.endpoint})`);
  }

  // Initialize browser manager and register browser tools (if enabled)
  let browserManager: BrowserManager | undefined;
  if (config.browser?.enabled) {
    browserManager = new BrowserManager({
      executablePath: config.browser.executable_path,
      wsEndpoint: config.browser.ws_endpoint,
      headless: config.browser.headless,
      maxPages: config.browser.max_pages,
      defaultTimeout: config.browser.default_timeout,
    });

    for (const tool of createBrowserTools(browserManager)) {
      toolRegistry.register(tool);
    }
    console.log(`Browser tools enabled (headless=${config.browser.headless})`);

    lifecycle.onShutdown(async () => {
      await browserManager!.shutdown();
      console.log('Browser manager stopped');
    });
  }

  const toolExecutor = new ToolExecutor(toolRegistry, hookEngine);

  // Initialize tool policy from config
  const toolPolicy = new ToolPolicy(config.tools);
  toolRegistry.setPolicy(toolPolicy);

  const effectiveProfile = toolPolicy.getEffectiveProfile();
  if (effectiveProfile !== 'full') {
    console.log(`Tool policy: profile=${effectiveProfile}, deny=[${config.tools.deny.join(', ')}]`);
  }

  return { toolRegistry, toolExecutor, browserManager };
}
