import type { AgentConfig } from '../runtime/agent.js';
import type { NetaClawSubagentTask } from '../service/subagent.js';

export const SUBAGENT_WORKER_PROTOCOL_VERSION = 1;

export interface SubagentWorkerPolicy {
  workspaceRoots?: string[];
  allowShell?: boolean;
  readonly?: boolean;
}

export interface SubagentToolManifestItem {
  name: string;
  visibility: 'internal' | 'tool' | 'skill';
  capability: 'text' | 'vision' | 'multimodal';
  kind: 'builtin' | 'memory' | 'skill' | 'delegation' | 'admin' | 'custom';
  executionMode: 'parallel' | 'sequential';
  supportedInWorker: boolean;
  workerRoutingHint?: 'local' | 'main-process-proxy' | 'disabled';
  requiresShell?: boolean;
  requiresWrite?: boolean;
  requiresSkillScope?: boolean;
}

export interface SubagentTaskEnvelope {
  protocolVersion: typeof SUBAGENT_WORKER_PROTOCOL_VERSION;
  runId: string;
  sessionId: string;
  parentMessageId?: number;
  parentToolCallId?: string | null;
  parentAgentId?: number | null;
  parentEntryId?: string | null;
  task: NetaClawSubagentTask;
  agentConfig: AgentConfig;
  toolNames: string[];
  toolManifest?: SubagentToolManifestItem[];
  policy?: SubagentWorkerPolicy;
  timeoutMs?: number;
  metadata?: Record<string, unknown>;
}

export interface SubagentWorkerResult {
  finalContent: string;
  finalOutput?: string;
  toolResults?: Array<{
    name: string;
    label?: string;
    result: string;
    toolCallId?: string;
  }>;
  usage?: {
    inputTokens?: number;
    outputTokens?: number;
  };
  toolCallCount?: number;
  metadata?: Record<string, unknown>;
}

export interface SubagentProxyToolCallEvent {
  type: 'proxy_tool_call';
  runId: string;
  timestamp: string;
  requestId: string;
  name: string;
  toolCallId: string;
  args: Record<string, unknown>;
}

export type SubagentParentMessage =
  | {
      type: 'proxy_tool_result';
      runId: string;
      requestId: string;
      result: string;
    }
  | {
      type: 'proxy_tool_error';
      runId: string;
      requestId: string;
      error: string;
    };

export type SubagentWorkerEvent =
  | {
      type: 'run_start';
      runId: string;
      timestamp: string;
      envelope?: Pick<
        SubagentTaskEnvelope,
        'sessionId' | 'parentMessageId' | 'parentToolCallId' | 'parentAgentId' | 'parentEntryId'
      >;
    }
  | {
      type: 'token' | 'thinking' | 'log';
      runId: string;
      timestamp: string;
      text: string;
    }
  | {
      type: 'tool_call';
      runId: string;
      timestamp: string;
      name: string;
      label?: string;
      args?: Record<string, unknown>;
      toolCallId?: string;
    }
  | {
      type: 'tool_result';
      runId: string;
      timestamp: string;
      name: string;
      label?: string;
      result: string;
      toolCallId?: string;
    }
  | SubagentProxyToolCallEvent
  | {
      type: 'run_end';
      runId: string;
      timestamp: string;
      result: SubagentWorkerResult;
    }
  | {
      type: 'run_error';
      runId: string;
      timestamp: string;
      error: string;
      details?: Record<string, unknown>;
    };

export type SubagentWorkerEventHandler = (event: SubagentWorkerEvent) => void;
