/**
 * Rainmaker Intelligence Engine (RIE) — Core Type Definitions
 *
 * All shared interfaces for the unified forecast intelligence pipeline.
 */

// ── Signal System ──────────────────────────────────────────────────────

export interface SignalResult {
  signalId: string;         // e.g., 'piff', 'digimon', 'dvp', 'kenpom', 'corner_scout', 'rag'
  score: number;            // normalized 0-1 confidence
  weight: number;           // assigned weight for this matchup (set by strategy)
  available: boolean;       // whether the signal had usable data
  rawData: any;             // original payload for audit/display
  metadata: {
    latencyMs: number;
    source: 'file' | 'db' | 'api' | 'cache';
    freshness: string;      // ISO date of underlying data
  };
}

export interface Signal {
  id: string;
  name: string;
  supportedLeagues: string[];  // ['nba', 'ncaab'] or ['*'] for all
  collect(ctx: MatchupContext): Promise<SignalResult>;
}

// ── Matchup Context ────────────────────────────────────────────────────

export interface MatchupContext {
  league: string;
  homeTeam: string;         // full name from SGO
  awayTeam: string;
  homeShort: string;        // 3-letter abbreviation
  awayShort: string;
  startsAt: string;         // ISO datetime
  eventId: string;
  event?: any;              // raw SGO event object
}

// ── Strategy ───────────────────────────────────────────────────────────

export interface StrategyProfile {
  league: string;
  signalWeights: Record<string, number>;   // signalId -> weight (should sum to ~1.0)
  requiredSignals: string[];               // must have data
  optionalSignals: string[];               // nice-to-have
  ragQueries: string[];                    // RAG query templates
}

// ── RAG Integration ────────────────────────────────────────────────────

export interface RagInsight {
  query: string;
  topChunks: Array<{
    content: string;
    source: string;
    score: number;
  }>;
  applicableTheory: string;  // one-line summary of the relevant concept
  cached: boolean;
}

// ── Intelligence Output ────────────────────────────────────────────────

export interface IntelligenceResult {
  compositeConfidence: number;   // 0-1
  stormCategory: number;         // 1-5
  signals: SignalResult[];
  strategyProfile: StrategyProfile;
  ragInsights: RagInsight[];
  edgeBreakdown: Record<string, { score: number; weight: number; contribution: number }>;
  inputQuality: InputQuality;
  compositeVersion: string;
}

export interface InputQuality {
  piff: Grade;
  dvp: Grade;
  digimon: Grade;
  odds: Grade;
  rag: Grade;
  overall: Grade;
}

export type Grade = 'A' | 'B' | 'C' | 'D' | 'N/A';

// ── Existing Backward-Compat ───────────────────────────────────────────
// CompositeResult shape from composite-score.ts that downstream code expects

export interface LegacyCompositeResult {
  compositeConfidence: number;
  stormCategory: number;
  modelSignals: {
    grok: { confidence: number; valueRating: number } | null;
    piff: { avgEdge: number; legCount: number; topPicks: Array<{ name: string; stat: string; line: number; edge: number; direction: string; tier: string; szn_hr?: number }> } | null;
    digimon: { available: boolean; lockCount: number; avgMissRate: number; topPicks: Array<{ player: string; prop: string; line: number; missRate: number; verdict: string }> } | null;
    dvp: { home: Record<string, Array<{ stat: string; rank: number; tier: string }>> | null; away: Record<string, Array<{ stat: string; rank: number; tier: string }>> | null } | null;
  };
  edgeBreakdown: {
    grokScore: number;
    piffScore: number;
    digimonScore: number | null;
    cornerScoutScore?: number;
    fangraphsScore?: number;
    kenpomScore?: number;
    kenpomMOV?: number;
    weights: Record<string, number>;
  };
  compositeVersion: string;
  rieSignals?: SignalResult[];
  ragInsights?: RagInsight[];
  strategyId?: string;
}
