/**
 * Intelligence routes — /api/symbols/*, /api/impact/*, /api/blueprint/*
 */

export function setupIntelligenceRoutes(app, server) {
  // --- Symbols ---
  app.get('/api/symbols/definition/:name', (req, res) => {
    const result = server.symbolIndex?.getDefinition(req.params.name);
    res.json(result || { error: 'Symbol not found' });
  });

  app.get('/api/symbols/references/:name', (req, res) => {
    const result = server.symbolIndex?.getReferences(req.params.name);
    res.json(result || []);
  });

  app.get('/api/symbols/complete', (req, res) => {
    const prefix = req.query.prefix || '';
    const result = server.symbolIndex?.complete(prefix) || [];
    res.json(result);
  });

  app.get('/api/symbols/outline', (req, res) => {
    const filePath = req.query.path;
    if (!filePath) return res.status(400).json({ error: 'path required' });
    const result = server.symbolIndex?.getOutline(filePath) || [];
    res.json(result);
  });

  app.get('/api/symbols/stats', (req, res) => {
    const stats = server.symbolIndex?.getStats() || { totalSymbols: 0, totalReferences: 0 };
    res.json(stats);
  });

  // --- Impact Analysis ---
  app.post('/api/impact/scan', (req, res) => {
    try {
      const result = server.impactAnalyzer?.analyze(req.body);
      res.json(result || { impacts: [] });
    } catch (e) { res.json({ error: e.message }); }
  });

  app.post('/api/impact/rename', (req, res) => {
    try {
      const result = server.impactAnalyzer?.analyzeRename(req.body);
      res.json(result || { impacts: [] });
    } catch (e) { res.json({ error: e.message }); }
  });

  // --- Blueprint Context ---
  app.get('/api/blueprint/status', (req, res) => {
    const bp = server.blueprintContext;
    res.json({
      hasBlueprints: bp?.hasBlueprints() || false,
      hasPRD: !!bp?.prd,
      hasServiceMap: !!bp?.serviceMap,
      hasUIMap: !!bp?.uiMap,
    });
  });

  app.get('/api/blueprint/context', (req, res) => {
    const bp = server.blueprintContext;
    res.json({
      prd: bp?.prd || null,
      serviceMap: bp?.serviceMap || null,
      uiMap: bp?.uiMap || null,
    });
  });
}
