/**
 * 2025-2026 Season Backtesting Audit - 20 Action Queries
 * Tests the system's ability to handle strategy backtesting and projections
 */

const BASE_URL = 'http://localhost:3000';

interface AuditResult {
  question: string;
  category: string;
  success: boolean;
  confidence: number;
  coverage: number;
  latency: number;
  hasProjection: boolean;
  hasRiskAssessment: boolean;
  hasRecommendation: boolean;
  responsePreview: string;
  error?: string;
}

const BACKTEST_QUERIES = {
  'NEWBIE': [
    'Backtest betting on NBA home favorites with spreads of -5 or more for the 2025-2026 season. What would the expected win rate and ROI be based on historical data?',
    'Test this NFL strategy for 2025-2026: Bet on underdogs getting 7+ points in divisional games. What should I expect based on past seasons?',
    'How would betting the OVER on NBA games with totals above 235 perform in the 2025-2026 season? Use historical averages.',
    'Show me the projected performance of betting against MLB teams on 3+ game losing streaks for the 2025 season.',
    'Backtest NHL home underdogs (+1.5 goals or less) for the 2025-2026 season. What\'s the historical success rate?',
    'Test betting only on Monday Night Football games in the 2025 NFL season. What\'s the expected ROI?',
  ],
  'BEGINNER': [
    'For the 2025-2026 NBA season, test a revenge game strategy: Bet on teams that lost the previous matchup by 15+ points.',
    'Backtest a public sentiment strategy for NFL 2025: Bet against teams with 70%+ public money. What\'s the historical edge?',
    'Project this NCAAF strategy for 2025: Bet on ranked home underdogs against unranked teams. Use 5-year historical data.',
    'Test a weather-based strategy for MLB 2025: Bet UNDER in games with wind blowing in at 15+ mph. Use historical weather correlation.',
    'For the 2025-2026 NHL season, test betting on teams playing their 3rd game in 4 nights when they\'re underdogs.',
    'Backtest a halftime strategy for NBA 2025-2026: Bet the 2nd half OVER when 1st half total is under 110 points.',
    'Project performance of betting NCAA basketball road teams getting 4+ points in conference play for 2025-2026.',
  ],
  'INTERMEDIATE': [
    'For the 2025 NFL season, test a dynamic staking system: Increase bet size by 25% after 2 consecutive wins, reset after a loss. Compare to flat betting.',
    'Backtest a line movement strategy for NBA 2025-2026: Only bet when the spread moves 2+ points in your favor pre-game.',
    'Project this combined strategy for MLB 2025: Home favorites AND coming off a win by 5+ runs. What\'s the expected win rate?',
    'Test a player prop system for NHL 2025-2026: Bet OVER on star players point totals when they\'re on back-to-back games.',
    'For the 2025-2026 college basketball season, backtest betting on small-conference home underdogs in non-conference games.',
    'Project a stop-loss system for NFL 2025: Stop betting after 4 consecutive losses, resume after a win. How does this affect ROI vs max drawdown?',
    'Test a correlated parlay strategy for NBA 2025-2026: Same-game parlay of moneyline and OVER. Compare to individual bets on each.',
  ],
};

async function runQuery(question: string): Promise<{
  success: boolean;
  confidence: number;
  coverage: number;
  latency: number;
  response: any;
  error?: string;
}> {
  const startTime = Date.now();

  try {
    const response = await fetch(`${BASE_URL}/api/chat?internal=1`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        message: question,
        domain: 'sports',
        conversationId: `backtest-audit-${Date.now()}`,
      }),
    });

    const latency = (Date.now() - startTime) / 1000;

    if (!response.ok) {
      return {
        success: false,
        confidence: 0,
        coverage: 0,
        latency,
        response: null,
        error: `HTTP ${response.status}`,
      };
    }

    const data = await response.json();
    const dataSheriff = data.dataSheriff || {};

    return {
      success: true,
      confidence: dataSheriff.confidence || 0,
      coverage: dataSheriff.coverage || 0,
      latency,
      response: data,
    };
  } catch (err: any) {
    return {
      success: false,
      confidence: 0,
      coverage: 0,
      latency: (Date.now() - startTime) / 1000,
      response: null,
      error: err.message,
    };
  }
}

function analyzeResponse(response: any): {
  hasProjection: boolean;
  hasRiskAssessment: boolean;
  hasRecommendation: boolean;
  preview: string;
} {
  const content = response?.message?.content || '';
  const contentLower = content.toLowerCase();

  // Check for projection elements
  const hasProjection =
    contentLower.includes('project') ||
    contentLower.includes('expect') ||
    contentLower.includes('win rate') ||
    contentLower.includes('roi') ||
    contentLower.includes('historical') ||
    contentLower.includes('%');

  // Check for risk assessment
  const hasRiskAssessment =
    contentLower.includes('risk') ||
    contentLower.includes('drawdown') ||
    contentLower.includes('variance') ||
    contentLower.includes('losing streak') ||
    contentLower.includes('confidence');

  // Check for recommendations
  const hasRecommendation =
    contentLower.includes('recommend') ||
    contentLower.includes('suggest') ||
    contentLower.includes('consider') ||
    contentLower.includes('stake') ||
    contentLower.includes('avoid');

  // Get preview
  const preview = content.substring(0, 100).replace(/\n/g, ' ');

  return { hasProjection, hasRiskAssessment, hasRecommendation, preview };
}

function sleep(ms: number): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function runAudit() {
  console.log('Starting 20-question backtesting audit...');
  console.log('======================================================================');
  console.log('   BACKTESTING AUDIT - 2025-2026 SEASON STRATEGIES');
  console.log('   Testing Strategy Parsing, Projections & Risk Analysis');
  console.log('======================================================================\n');

  const results: AuditResult[] = [];
  let questionNum = 0;
  const totalQuestions = Object.values(BACKTEST_QUERIES).flat().length;

  for (const [category, questions] of Object.entries(BACKTEST_QUERIES)) {
    console.log('──────────────────────────────────────────────────────────────────────');
    console.log(`  ${category}`);
    console.log('──────────────────────────────────────────────────────────────────────');

    for (const question of questions) {
      questionNum++;
      const shortQ = question.substring(0, 45).replace(/\n/g, ' ');
      process.stdout.write(`[${questionNum}/${totalQuestions}] ${shortQ}...`);

      // Add delay between requests to avoid rate limiting
      if (questionNum > 1) {
        await sleep(5000);
      }

      const result = await runQuery(question);

      let analysis = { hasProjection: false, hasRiskAssessment: false, hasRecommendation: false, preview: '' };
      if (result.success && result.response) {
        analysis = analyzeResponse(result.response);
      }

      const auditResult: AuditResult = {
        question,
        category,
        success: result.success,
        confidence: result.confidence,
        coverage: result.coverage,
        latency: result.latency,
        hasProjection: analysis.hasProjection,
        hasRiskAssessment: analysis.hasRiskAssessment,
        hasRecommendation: analysis.hasRecommendation,
        responsePreview: analysis.preview,
        error: result.error,
      };
      results.push(auditResult);

      // Print result
      const status = result.success ? 'OK' : 'FAIL';
      const conf = Math.round(result.confidence * 100);
      const projIcon = analysis.hasProjection ? '📊' : '  ';
      const riskIcon = analysis.hasRiskAssessment ? '⚠️' : '  ';
      const recIcon = analysis.hasRecommendation ? '💡' : '  ';

      console.log(` ${status} ${conf}% ${projIcon}${riskIcon}${recIcon} ${result.latency.toFixed(1)}s`);

      if (result.error) {
        console.log(`      Error: ${result.error}`);
      }
    }
    console.log('');
  }

  // Print summary
  printSummary(results);

  return results;
}

function printSummary(results: AuditResult[]) {
  console.log('======================================================================');
  console.log('   SUMMARY');
  console.log('======================================================================\n');

  const successful = results.filter(r => r.success);
  const withProjections = results.filter(r => r.hasProjection);
  const withRisk = results.filter(r => r.hasRiskAssessment);
  const withRec = results.filter(r => r.hasRecommendation);

  const avgConf = successful.length > 0
    ? successful.reduce((sum, r) => sum + r.confidence, 0) / successful.length
    : 0;
  const avgCov = successful.length > 0
    ? successful.reduce((sum, r) => sum + r.coverage, 0) / successful.length
    : 0;
  const avgLatency = results.reduce((sum, r) => sum + r.latency, 0) / results.length;

  console.log('  Overall:');
  console.log(`    Successful:      ${successful.length}/${results.length} (${Math.round(successful.length/results.length*100)}%)`);
  console.log(`    Avg Confidence:  ${Math.round(avgConf * 100)}%`);
  console.log(`    Avg Coverage:    ${Math.round(avgCov * 100)}%`);
  console.log(`    Avg Latency:     ${avgLatency.toFixed(1)}s`);
  console.log('');
  console.log('  Response Quality:');
  console.log(`    Has Projections:     ${withProjections.length}/${results.length} (📊)`);
  console.log(`    Has Risk Analysis:   ${withRisk.length}/${results.length} (⚠️)`);
  console.log(`    Has Recommendations: ${withRec.length}/${results.length} (💡)`);
  console.log('');

  // By category
  console.log('  By Category:');
  for (const category of Object.keys(BACKTEST_QUERIES)) {
    const catResults = results.filter(r => r.category === category);
    const catSuccess = catResults.filter(r => r.success);
    const catConf = catSuccess.length > 0
      ? catSuccess.reduce((sum, r) => sum + r.confidence, 0) / catSuccess.length
      : 0;
    const catProj = catResults.filter(r => r.hasProjection).length;
    console.log(`    ${category.padEnd(12)}: ${catSuccess.length}/${catResults.length} success, ${Math.round(catConf*100)}% conf, ${catProj}/${catResults.length} projections`);
  }

  console.log('');
  console.log('  Legend: 📊=Projection ⚠️=Risk Analysis 💡=Recommendation');
  console.log('');
  console.log('======================================================================');
}

// Run the audit
runAudit().catch(console.error);
