/**
 * agent-kb.js — Render V2 docs as token-efficient JSON for AI agents.
 * 
 * Output: A single JSON file mirroring the markdown doc structure exactly, but
 * stripped of markdown formatting to save context tokens during agent eval.
 */

const fs = require('fs');
const path = require('path');

/**
 * Build the agent knowledge base from the exact same data used to render the markdown docs.
 */
function buildAgentKB(graph, srcRoot, helmCharts, subs, contractsResult, patterns, impactResults) {
  const kb = {
    _meta: {
      generated: new Date().toISOString(),
      format: 'dev-intel-agent-kb-v2',
      description: 'Token-efficient rendering of Dev Intel V2 documentation for AI agents.',
    },
    
    reference: {
      systemArchitecture: {
        summary: {
          subsystemCount: subs.subsystems.length,
          helmChartCount: helmCharts.length,
          contractCount: contractsResult.contracts.length,
          crossCuttingConcerns: subs.crossCutting,
        },
        patterns: patterns,
      },
      
      subsystems: subs.subsystems.map(sub => {
        const outgoing = Object.entries(subs.dependencyMatrix)
          .filter(([k]) => k.startsWith(sub.name + '→'))
          .map(([k, v]) => ({ target: k.split('→')[1], calls: v.calls, imports: v.imports }));
        const incoming = Object.entries(subs.dependencyMatrix)
          .filter(([k]) => k.endsWith('→' + sub.name))
          .map(([k, v]) => ({ source: k.split('→')[0], calls: v.calls, imports: v.imports }));
        
        return {
          name: sub.name,
          kind: sub.kind,
          files: sub.files,
          entities: sub.entities,
          publicExports: sub.publicExports,
          dependencies: {
            outgoing,
            incoming,
          },
          contracts: (contractsResult.bySubsystem[sub.name] || []).map(c => ({
            name: c.name,
            type: c.type,
          })),
        };
      }),

      helm: {
        charts: helmCharts.map(c => ({
          name: c.chart.name,
          version: c.chart.version,
          appVersion: c.chart.appVersion || null,
          path: c.dir,
          dependencies: c.chart.dependencies.map(d => d.name),
          resourceCount: c.templates.resources.length,
          valuesKeys: c.values.keys.map(k => k.name),
          interactions: c.interactions.map(i => ({ type: i.type, target: i.target }))
        })),
        syncWaves: patterns.syncWaves,
      },

      contracts: contractsResult.contracts.map(c => ({
        name: c.name,
        type: c.type,
        id: c.id,
      })),
    }
  };

  return kb;
}

module.exports = { buildAgentKB };
