/**
 * Shared composition for Cortex instruction injection surfaces.
 *
 * The managed Cortex instruction artifact is generated by the Myco agent and
 * remains the source of truth. Event-specific injection paths may wrap that
 * artifact with a small stable frame, but they should not fork their own
 * instruction content.
 */

export const SUBAGENT_CORTEX_GUIDANCE = [
  'You are a delegated subagent working inside a Myco-connected project.',
  'Follow these managed Cortex instructions as current project guidance.',
  'Apply them to your assigned task, and defer broad orchestration decisions back to the parent agent.',
].join('\n');

export const CLI_TOOL_TRANSPORT_DIRECTIVE = [
  '**Myco tool transport (this host):** No Myco MCP server is installed here.',
  'Call every Myco tool — including any `myco_*` tool named below or in any skill —',
  "via your shell: `myco tool call <tool> --json --input '<json-args>'`,",
  'run from the project working directory (this resolves project tenancy automatically).',
  'Example: `myco tool call myco_cortex --json --input \'{"op":"instructions"}\'`.',
].join('\n');

export type CortexInjectionSurface = 'session-start' | 'subagent-start';

export interface CortexInjectionContext {
  surface: CortexInjectionSurface;
  text: string;
}

export function composeCortexInstructionInjection(
  cortexContent: string,
  surface: CortexInjectionSurface,
  options: { cliToolTransport?: boolean } = {},
): CortexInjectionContext | null {
  const trimmed = cortexContent.trim();
  if (!trimmed) return null;

  const directive = options.cliToolTransport ? `${CLI_TOOL_TRANSPORT_DIRECTIVE}\n\n` : '';

  if (surface === 'subagent-start') {
    return {
      surface,
      text: `${directive}${SUBAGENT_CORTEX_GUIDANCE}\n\n${trimmed}`,
    };
  }

  return { surface, text: `${directive}${trimmed}` };
}
