/**
 * RIE Strategy: NCAAB — Enhanced v2
 *
 * KenPom-heavy with Grok. No PIFF (college prop data is sparse).
 * Tournament mode uses seed/experience/conference data for upset calibration.
 */

import { BaseStrategy } from './base.strategy';
import { StrategyProfile, SignalResult, RagInsight } from '../types';

export class NcaabStrategy extends BaseStrategy {
  league = 'ncaab';

  getProfile(): StrategyProfile {
    return {
      league: 'ncaab',
      signalWeights: {
        grok: 0.40,
        kenpom: 0.45,
        dvp: 0.05,
        rag: 0.10,
      },
      requiredSignals: ['grok'],
      optionalSignals: ['kenpom', 'dvp', 'rag'],
      ragQueries: [
        'college basketball power ratings margin of victory prediction model',
        'NCAA tournament variance upset probability seed analysis',
      ],
    };
  }

  adjustComposite(base: number, signals: SignalResult[], ragInsights: RagInsight[]): number {
    let adjusted = base;

    const kpSignal = signals.find(s => s.signalId === 'kenpom');

    // === Tournament mode (March-April) ===
    const now = new Date();
    const month = now.getMonth() + 1;
    if (month === 3 || month === 4) {
      const distFromCenter = Math.abs(adjusted - 0.5);

      if (distFromCenter > 0.15) {
        // Base tournament uncertainty: pull extreme confidence toward center
        let penalty = distFromCenter * 0.08;

        // Reduce penalty if KenPom signal is strong and both teams are rated
        if (kpSignal?.available && kpSignal.rawData?.gamesPlayed >= 25) {
          const kpDimensions = kpSignal.rawData?.dimensions;

          // If fanmatch agrees with composite direction, reduce penalty
          if (kpDimensions?.fmScore != null) {
            const fmAgrees = (kpDimensions.fmScore > 0.5) === (adjusted > 0.5);
            if (fmAgrees) penalty *= 0.6;
          }

          // If MOV is decisive (>8 pts), KenPom is confident — reduce penalty
          const mov = Math.abs(kpSignal.rawData?.predictedMOV || 0);
          if (mov > 8) penalty *= 0.7;
        }

        adjusted = adjusted > 0.5
          ? adjusted - penalty
          : adjusted + penalty;
      }
    }

    // === Agreement bonus ===
    // If KenPom and Grok agree on direction, boost confidence slightly
    const grokSignal = signals.find(s => s.signalId === 'grok');
    if (kpSignal?.available && grokSignal?.available) {
      const kpDirection = kpSignal.score > 0.5; // home favored
      const grokDirection = grokSignal.score > 0.5;

      if (kpDirection === grokDirection) {
        // Agreement: boost by up to 3%
        const agreement = Math.min(
          Math.abs(kpSignal.score - 0.5),
          Math.abs(grokSignal.score - 0.5),
        );
        adjusted = adjusted > 0.5
          ? adjusted + agreement * 0.06
          : adjusted - agreement * 0.06;
      } else {
        // Disagreement: dampen toward 0.5 by up to 2%
        const disagreement = Math.min(
          Math.abs(kpSignal.score - 0.5),
          Math.abs(grokSignal.score - 0.5),
        );
        adjusted = adjusted > 0.5
          ? adjusted - disagreement * 0.04
          : adjusted + disagreement * 0.04;
      }
    }

    return Math.max(0.02, Math.min(0.98, adjusted));
  }
}
