import type { Tool, ToolResult } from '../../types.js';
import type { ProcessManager } from './manager.js';

interface ProcessOutputArgs {
  id: string;
}

export function createProcessOutputTool(manager: ProcessManager): Tool {
  return {
    name: 'process.output',
    description: 'Read recent stdout/stderr output from a background process. Returns the last ~64KB of output.',
    inputSchema: {
      type: 'object',
      properties: {
        id: { type: 'string', description: 'Process ID (e.g. "proc_1")' },
      },
      required: ['id'],
    },
    execute: async (rawArgs: unknown): Promise<ToolResult> => {
      const args = rawArgs as ProcessOutputArgs;
      try {
        const output = manager.getOutput(args.id);
        if (!output) {
          return { success: true, output: '(no output yet)' };
        }
        return { success: true, output };
      } catch (error) {
        return {
          success: false,
          output: '',
          error: error instanceof Error ? error.message : 'Failed to get process output',
        };
      }
    },
  };
}
