/**
 * Constants for Context Management
 *
 * Shared constants used by both main chat agent and sub-agents.
 */

/**
 * Base token count for system instructions.
 * This should be updated if instructions change significantly.
 * After P1 optimization (moving detailed guidance to tool descriptions):
 * ~2,800 chars = ~700 tokens (reduced from ~17,500 chars / ~4,400 tokens)
 */
export const SYSTEM_INSTRUCTIONS_TOKENS = 800;

/**
 * Safety margin for context limit (75% of max).
 * Leaves room for output tokens and tool call overhead.
 */
export const CONTEXT_SAFETY_MARGIN = 0.75;

/**
 * Default model context limit in tokens.
 * Most modern models support 128K+, but we use a conservative default.
 */
export const DEFAULT_MODEL_CONTEXT_LIMIT = 128000;

/**
 * Number of recent messages to load for context size estimation.
 * Used only by estimateContextSize / checkAndSummarizeIfNeeded as a sampling heuristic.
 */
export const DEFAULT_RECENT_MESSAGES = 20;

/**
 * Maximum token budget for conversation history in the context window.
 * The actual loading uses token-aware priority selection: conversational messages
 * (user/assistant/system) are loaded first, then tool messages fill the remainder.
 */
export const DEFAULT_MAX_HISTORY_TOKENS = 25_000;

/**
 * Reserve tokens for model output generation.
 * This ensures the model has room to generate a complete response.
 */
export const OUTPUT_RESERVE = 4096;

/**
 * Estimate for recent conversation history tokens.
 * Used by ContextBuilder.build() for budget calculations.
 * Aligned with DEFAULT_MAX_HISTORY_TOKENS.
 */
export const RECENT_MESSAGES_TOKEN_ESTIMATE = 25_000;

/**
 * Threshold for considering a message as "large" (in tokens).
 * Large messages may be skipped when budget is tight.
 */
export const LARGE_MESSAGE_THRESHOLD = 2000;

/**
 * Threshold for triggering summarization.
 * When context usage exceeds this ratio, async summarization is triggered.
 */
export const SUMMARIZATION_THRESHOLD = 0.65;

/**
 * Max time for the recovery generation when the main generation times out.
 * The recovery runs without tools, so it should be fast.
 */
export const RECOVERY_TIMEOUT_MS = 60_000;

/**
 * Agent-specific context configurations
 *
 * All agents are treated as equal and independent - they can be used as
 * standalone entry points or as sub-agents delegated by the chat agent.
 * Each agent has full context management capabilities including:
 * - Complete message history loading
 * - XML-structured context formatting
 * - Summarization support for long conversations
 */
export const AGENT_CONTEXT_CONFIGS = {
  /** Main chat agent - full context management */
  chat: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: OUTPUT_RESERVE,
    enableSummarization: true,
    timeoutMs: 1_200_000,
  },
  /** Web assistant - independent agent for web operations */
  web: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 300_000,
  },
  /** File assistant - independent agent for file operations */
  file: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 4096,
    enableSummarization: true,
    timeoutMs: 180_000,
  },
  /** Integration assistant - independent agent for external system operations */
  integration: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 180_000,
  },
  /** Workflow assistant - independent agent for workflow operations */
  workflow: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 240_000,
  },
  /** CRM assistant - independent agent for CRM operations */
  crm: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 180_000,
  },
  /** Agent - user-configurable agent with DB-stored config */
  custom: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 420_000,
  },
  /**
   * Agent invoked via the OpenAI-compatible webhook sub-path
   * (`POST /api/agents/wh/<TOKEN>/chat/completions`). Same runtime budgets
   * as `custom`; the distinct tag lets audit / analytics separate the two
   * wire formats without breaking per-agent dashboards.
   */
  openai_webhook: {
    modelContextLimit: DEFAULT_MODEL_CONTEXT_LIMIT,
    maxHistoryTokens: DEFAULT_MAX_HISTORY_TOKENS,
    outputReserve: 2048,
    enableSummarization: true,
    timeoutMs: 420_000,
  },
} as const;

export type AgentType = keyof typeof AGENT_CONTEXT_CONFIGS;
