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

interface ProcessStartArgs {
  command: string;
  cwd?: string;
}

export function createProcessStartTool(manager: ProcessManager): Tool {
  return {
    name: 'process.start',
    description: 'Start a command in the background. Returns a process ID that can be used with process.status, process.output, and process.kill.',
    inputSchema: {
      type: 'object',
      properties: {
        command: { type: 'string', description: 'The shell command to run in the background' },
        cwd: { type: 'string', description: 'Working directory (optional)' },
      },
      required: ['command'],
    },
    execute: async (rawArgs: unknown): Promise<ToolResult> => {
      const args = rawArgs as ProcessStartArgs;
      try {
        const proc = manager.start(args.command, args.cwd);
        return {
          success: true,
          output: `Started background process ${proc.id} (PID ${proc.pid})\nCommand: ${proc.command}`,
        };
      } catch (error) {
        return {
          success: false,
          output: '',
          error: error instanceof Error ? error.message : 'Failed to start process',
        };
      }
    },
  };
}
