/**
 * RIE Signal: RAG Knowledge Base
 *
 * Queries the 24K+ chunk sports analytics knowledge base for relevant
 * statistical theory and domain expertise per matchup.
 *
 * The RAG signal provides contextual enrichment — grounding LLM analysis
 * in peer-reviewed sports analytics theory (CLV, Poisson models, DVP theory,
 * Bayesian inference, sabermetrics, soccermatics, etc.)
 */

import { Signal, SignalResult, MatchupContext } from '../types';
import { queryRag, computeRagRelevanceScore } from '../rag-client';

// Sport-specific RAG query templates
const RAG_QUERIES: Record<string, string[]> = {
  nba: [
    'NBA player props defense vs position expected value edge',
    'basketball scoring pace efficiency matchup analysis model',
  ],
  ncaab: [
    'college basketball power ratings margin of victory prediction model',
    'NCAA tournament variance upset probability seed analysis',
  ],
  nhl: [
    'hockey goaltender save percentage matchup analysis model',
    'NHL shot quality expected goals scoring prediction',
  ],
  mlb: [
    'baseball pitcher batter matchup park factor run scoring model',
    'MLB sabermetrics expected run production analysis',
  ],
  epl: [
    'football expected goals poisson distribution match prediction',
    'soccer corner kicks set pieces analysis prediction model',
  ],
  la_liga: [
    'football expected goals poisson distribution match prediction',
    'La Liga tactical analysis possession scoring model',
  ],
  bundesliga: [
    'football expected goals poisson distribution match prediction',
    'Bundesliga high scoring tactical analysis model',
  ],
  serie_a: [
    'football expected goals poisson distribution match prediction',
    'Serie A defensive tactical analysis low scoring model',
  ],
  ligue_1: [
    'football expected goals poisson distribution match prediction',
    'Ligue 1 dominance top team analysis model',
  ],
  champions_league: [
    'Champions League aggregate two-leg probability model',
    'European football elite competition tactical analysis',
  ],
  mma: [
    'combat sports fight prediction statistical model',
    'MMA fighter reach striking grappling matchup analysis',
  ],
};

// Default queries for any league
const DEFAULT_QUERIES = [
  'closing line value expected value sports betting edge detection',
  'statistical sports prediction model confidence calibration',
];

export const ragSignal: Signal = {
  id: 'rag',
  name: 'RAG Knowledge Base',
  supportedLeagues: ['*'],

  async collect(ctx: MatchupContext): Promise<SignalResult> {
    const start = Date.now();
    try {
      const queries = RAG_QUERIES[ctx.league.toLowerCase()] || DEFAULT_QUERIES;

      const insights = await queryRag({
        queries,
        league: ctx.league,
        homeShort: ctx.homeShort,
        awayShort: ctx.awayShort,
        k: 3,
      });

      const score = computeRagRelevanceScore(insights);
      const hasData = insights.some(i => i.topChunks.length > 0);

      // Build a concise theory summary from top chunks
      const theorySummaries = insights
        .filter(i => i.applicableTheory)
        .map(i => i.applicableTheory);

      return {
        signalId: 'rag',
        score,
        weight: 0,
        available: hasData,
        rawData: {
          queryCount: queries.length,
          chunkCount: insights.reduce((s, i) => s + i.topChunks.length, 0),
          cachedCount: insights.filter(i => i.cached).length,
          topSources: [...new Set(
            insights.flatMap(i => i.topChunks.map(c => c.source))
          )].slice(0, 5),
          theorySummaries,
          insights, // full data for Grok prompt injection
        },
        metadata: {
          latencyMs: Date.now() - start,
          source: 'api',
          freshness: new Date().toISOString(),
        },
      };
    } catch (err) {
      return {
        signalId: 'rag', score: 0.5, weight: 0, available: false,
        rawData: { error: String(err) },
        metadata: { latencyMs: Date.now() - start, source: 'api', freshness: '' },
      };
    }
  },
};
