'use strict';

/**
 * Model routing hint definitions for SigMap.
 *
 * Maps complexity tiers to:
 *   - example model names (vendor-agnostic labels used as hints)
 *   - task types that belong to each tier
 *   - estimated per-1K-token API cost (USD, illustrative — update as needed)
 *
 * These are embedded in the generated context file when `routing: true`
 * so that AI agents and developers know which model tier to invoke.
 */

const TIERS = {
  fast: {
    label: 'Fast (low-cost)',
    examples: 'claude-haiku-4-5, gpt-5-1-codex-mini, gemini-3-flash',
    tasks: [
      'Autocomplete and inline suggestions',
      'Edit config or markup files',
      'Fix typos and rename symbols',
      'Format, lint, and trivial style changes',
      'Explain a short utility function',
      'Generate simple shell scripts or Dockerfiles',
    ],
    costHint: '~$0.0008 / 1K tokens',
  },

  balanced: {
    label: 'Balanced (mid-tier)',
    examples: 'claude-sonnet-4-6, gpt-5-2, gemini-3-1-pro',
    tasks: [
      'Write unit or integration tests',
      'Implement a well-scoped feature function',
      'Debug a runtime error with stack trace',
      'Refactor a module (< 200 lines)',
      'Generate a PR description',
      'Explain a multi-function module',
    ],
    costHint: '~$0.003 / 1K tokens',
  },

  powerful: {
    label: 'Powerful (high-cost)',
    examples: 'claude-opus-4-6, gpt-5-4, gemini-2-5-pro',
    tasks: [
      'Cross-cutting architecture decisions',
      'Multi-file refactor spanning 5+ files',
      'Security audit (OWASP Top 10)',
      'Complex debugging across async boundaries',
      'Migration plan for a library/framework upgrade',
      'Designing a new module from requirements',
    ],
    costHint: '~$0.015 / 1K tokens',
  },
};

/**
 * Format the routing section as markdown to append to the context file.
 *
 * @param {{ fast: string[], balanced: string[], powerful: string[] }} groups
 *   Relative file paths grouped by tier (from classifier.classifyAll).
 * @returns {string} Markdown block to embed in the context output.
 */
function formatRoutingSection(groups) {
  const lines = [
    '',
    '---',
    '',
    '## Model routing hints',
    '<!-- Generated by SigMap routing module — update gen-context.config.json to disable -->',
    '',
    'Select the model tier based on the task complexity and the files involved.',
    '',
  ];

  for (const [tier, info] of Object.entries(TIERS)) {
    const files = groups[tier] || [];
    lines.push(`### ${info.label}`);
    lines.push(`**Examples:** ${info.examples}  `);
    lines.push(`**Cost:** ${info.costHint}`);
    lines.push('');
    lines.push('**Use for tasks like:**');
    for (const task of info.tasks) lines.push(`- ${task}`);
    lines.push('');
    if (files.length > 0) {
      lines.push('**Files in this tier:**');
      for (const f of files.slice(0, 15)) lines.push(`- \`${f}\``);
      if (files.length > 15) lines.push(`- … and ${files.length - 15} more`);
    } else {
      lines.push('**Files in this tier:** _(none detected)_');
    }
    lines.push('');
  }

  lines.push('> **Tip:** Run `node gen-context.js --routing` to regenerate routing hints.');
  lines.push('> See `docs/MODEL_ROUTING.md` for full routing guide and cost optimisation tips.');

  return lines.join('\n');
}

module.exports = { TIERS, formatRoutingSection };
