import { isAgentFeatureEnabled } from '../utils/agent-feature-enabled';

export const DOMAIN_TOOL_IDS = {
	WORKFLOWS: 'workflows',
	EVALS: 'evals',
	EVAL_CONFIG: 'eval-config',
	EXECUTIONS: 'executions',
	CREDENTIALS: 'credentials',
	DATA_TABLES: 'data-tables',
	WORKSPACE: 'workspace',
	RESEARCH: 'research',
	N8N_DOCS: 'n8n-docs',
	NODES: 'nodes',
	ASK_USER: 'ask-user',
	BUILD_WORKFLOW: 'build-workflow',
	PARSE_FILE: 'parse-file',
	AGENTS: 'agents',
	MCP_SERVERS: 'mcp-servers',
} as const;

/** Trace-only chain-typed child run emitted by `build-workflow` with the
 *  compiled workflow JSON — bookkeeping, not an agent-facing tool. Consumed by
 *  the eval harness (`langsmith-seed.ts`) so seed reconstruction can skip the
 *  SDK re-parse; excluded by name from rebuilt transcripts. */
export const COMPILED_WORKFLOW_TRACE_RUN_NAME = 'compiled-workflow';

/** Trace-only chain run carrying an agent's config + skills, for seed
 *  reconstruction — the agent counterpart of the event above. */
export const AGENT_SNAPSHOT_TRACE_RUN_NAME = 'agent-snapshot';

export const ORCHESTRATION_TOOL_IDS = {
	CREATE_TASKS: 'create-tasks',
	TASK_CONTROL: 'task-control',
	EVAL_SETUP_WITH_AGENT: 'eval-setup-with-agent',
	EVAL_DATA: 'eval-data',
	COMPLETE_CHECKPOINT: 'complete-checkpoint',
	VERIFY_BUILT_WORKFLOW: 'verify-built-workflow',
	REPORT_VERIFICATION_VERDICT: 'report-verification-verdict',
	APPLY_WORKFLOW_CREDENTIALS: 'apply-workflow-credentials',
	BUILD_AGENT: 'build-agent',
	LIST_AGENT_CAPABILITIES: 'list-agent-capabilities',
	GET_SESSION: 'get-session',
} as const;

export const WORKSPACE_TOOL_IDS = {
	WRITE_FILE: 'write-file',
} as const;

export const CREDENTIALS_TOOL_ID = DOMAIN_TOOL_IDS.CREDENTIALS;
export const DATA_TABLES_TOOL_ID = DOMAIN_TOOL_IDS.DATA_TABLES;
export const EVAL_CONFIG_TOOL_ID = DOMAIN_TOOL_IDS.EVAL_CONFIG;
export const ASK_USER_TOOL_ID = DOMAIN_TOOL_IDS.ASK_USER;
export const N8N_DOCS_TOOL_ID = DOMAIN_TOOL_IDS.N8N_DOCS;

export const ORCHESTRATION_TOOL_NAMES = new Set<string>(Object.values(ORCHESTRATION_TOOL_IDS));

export const ALWAYS_LOADED_TOOL_NAMES = new Set<string>([
	DOMAIN_TOOL_IDS.ASK_USER,
	DOMAIN_TOOL_IDS.CREDENTIALS,
	DOMAIN_TOOL_IDS.WORKFLOWS,
	DOMAIN_TOOL_IDS.EXECUTIONS,
	DOMAIN_TOOL_IDS.DATA_TABLES,
	DOMAIN_TOOL_IDS.PARSE_FILE,
	DOMAIN_TOOL_IDS.BUILD_WORKFLOW,
	DOMAIN_TOOL_IDS.NODES,
	ORCHESTRATION_TOOL_IDS.VERIFY_BUILT_WORKFLOW,
	DOMAIN_TOOL_IDS.RESEARCH,
	// Paired with RESEARCH on purpose: the research tool tells the agent to prefer
	// n8n's own docs for n8n questions, but deferring n8n-docs priced that route at
	// search_tools + load_tool while web search stayed one call away — so the agent
	// web-searched things the docs answer (INS-749).
	DOMAIN_TOOL_IDS.N8N_DOCS,
	DOMAIN_TOOL_IDS.AGENTS,
	// Deferring this one defeats its purpose: it exists for the case where
	// nothing is connected, which is exactly when `search_tools` has no MCP tool
	// to surface and the agent concludes the integration is unavailable.
	DOMAIN_TOOL_IDS.MCP_SERVERS,
	'web-search',
	'fetch-url',
	// build-agent is the primary route for agent-anchored intents; deferring it
	// costs 2 LLM rounds (search_tools + load_tool) and a prompt-cache rewrite
	// on every agent build.
	...(isAgentFeatureEnabled() ? [ORCHESTRATION_TOOL_IDS.BUILD_AGENT] : []),
	// Paired with build-agent: the model must be able to check supported agent
	// channels/capabilities during intent recognition — before committing to a
	// path — so an unsupported channel (e.g. WhatsApp) is explained rather than
	// improvised as a workflow. Deferring it costs a search_tools + load_tool
	// round-trip and lets the model fall back to workflow assumptions first.
	...(isAgentFeatureEnabled() ? [ORCHESTRATION_TOOL_IDS.LIST_AGENT_CAPABILITIES] : []),
]);

export const CHECKPOINT_FOLLOW_UP_TOOL_NAMES = new Set<string>([
	ORCHESTRATION_TOOL_IDS.COMPLETE_CHECKPOINT,
	DOMAIN_TOOL_IDS.EXECUTIONS,
]);
