import { PrismaClient } from '@prisma/client';
import * as dotenv from 'dotenv';
import axios from 'axios';

dotenv.config();

// BigInt serialization fix
(BigInt.prototype as any).toJSON = function () {
  return this.toString();
};

const prisma = new PrismaClient();

const matchIds = [
  '7cnm7h7qbsq2bbaxngusojh90',  // Club Brugge vs Anderlecht - TESTED ✅
  '7lmrfu2k1e2uxprxfxgaevcb8',  // Castellon vs Granada
  '3ko3otchy41d28rzxfpvl3d3o'  // SV Ried vs Altach
];

async function getPrediction(matchId: string) {
  try {
    console.log(`\n${'='.repeat(80)}`);
    console.log(`🔮 PREDICTION REQUEST: ${matchId}`);
    console.log('='.repeat(80));

    // Fetch match from database
    const match = await prisma.liveMatch.findUnique({
      where: { id: matchId },
      include: {
        homeTeam: true,
        awayTeam: true,
        league: true,
      },
    });

    if (!match) {
      console.log(`❌ Match not found: ${matchId}`);
      return null;
    }

    console.log(`📊 ${match.homeTeam?.name} vs ${match.awayTeam?.name}`);
    console.log(`🏆 League: ${match.league?.name}`);
    console.log(`📅 Match Time: ${new Date(Number(match.mstUtc)).toISOString()}`);

    // Send prediction request to AI Engine
    const aiEngineUrl = 'http://localhost:8007';
    const predictionUrl = `${aiEngineUrl}/v20plus/analyze/${matchId}`;

    console.log(`\n🤖 Sending to AI Engine: ${predictionUrl}`);

    const startTime = Date.now();
    const response = await axios.post(predictionUrl, {}, {
      timeout: 120000, // 2 minutes timeout
    });

    const elapsed = ((Date.now() - startTime) / 1000).toFixed(2);

    console.log(`✅ Prediction received in ${elapsed}s`);
    console.log(`\n${'='.repeat(80)}`);
    console.log(`📊 FULL PREDICTION JSON:`);
    console.log('='.repeat(80));
    console.log(JSON.stringify(response.data, null, 2));

    // Summary
    const pkg = response.data;
    if (pkg.main_pick) {
      console.log(`\n${'='.repeat(80)}`);
      console.log(`🎯 SUMMARY:`);
      console.log('='.repeat(80));
      console.log(`Main Pick: ${pkg.main_pick.market} → ${pkg.main_pick.pick}`);
      console.log(`Confidence: ${pkg.main_pick.confidence}%`);
      console.log(`Odds: ${pkg.main_pick.odds}`);
      console.log(`Bet Grade: ${pkg.main_pick.bet_grade}`);
      console.log(`Edge: ${pkg.main_pick.edge || 'N/A'}`);

      if (pkg.value_pick) {
        console.log(`\nValue Pick: ${pkg.value_pick.market} → ${pkg.value_pick.pick}`);
        console.log(`Confidence: ${pkg.value_pick.confidence}%`);
        console.log(`Odds: ${pkg.value_pick.odds}`);
      }

      if (pkg.bet_advice) {
        console.log(`\n💡 Bet Advice:`);
        console.log(`   Playable: ${pkg.bet_advice.playable}`);
        console.log(`   Stake: ${pkg.bet_advice.suggested_stake_units} units`);
        console.log(`   Reason: ${pkg.bet_advice.reason}`);
      }

      if (pkg.score_prediction) {
        console.log(`\n⚽ Score Prediction:`);
        console.log(`   FT: ${pkg.score_prediction.ft}`);
        console.log(`   HT: ${pkg.score_prediction.ht}`);
        console.log(`   xG: ${pkg.score_prediction.xg_home} - ${pkg.score_prediction.xg_away}`);
      }

      if (pkg.risk) {
        console.log(`\n⚠️ Risk Level: ${pkg.risk.level} (${pkg.risk.score})`);
        if (pkg.risk.warnings?.length > 0) {
          console.log(`   Warnings: ${pkg.risk.warnings.join(', ')}`);
        }
      }

      if (pkg.ai_commentary) {
        console.log(`\n💬 AI Commentary:`);
        console.log(`   ${pkg.ai_commentary}`);
      }
    }

    return response.data;

  } catch (error: any) {
    console.error(`❌ Error for match ${matchId}:`);
    if (error.response) {
      console.error(`   Status: ${error.response.status}`);
      console.error(`   Data: ${JSON.stringify(error.response.data, null, 2)}`);
    } else {
      console.error(`   Message: ${error.message}`);
    }
    return null;
  }
}

async function main() {
  console.log('🚀 VQWEN v3 Prediction Engine - Batch Analysis');
  console.log(`📡 AI Engine: ${process.env.AI_ENGINE_URL || 'http://localhost:8007'}`);
  console.log(`🎯 Matches: ${matchIds.length}`);

  const results: { matchId: string; success: boolean }[] = [];

  for (const matchId of matchIds) {
    const result = await getPrediction(matchId);
    if (result) {
      results.push({ matchId, success: true });
    } else {
      results.push({ matchId, success: false });
    }

    // Small delay between requests
    await new Promise(resolve => setTimeout(resolve, 1000));
  }

  console.log(`\n${'='.repeat(80)}`);
  console.log(`📊 BATCH SUMMARY:`);
  console.log('='.repeat(80));
  results.forEach((r, i) => {
    console.log(`${r.success ? '✅' : '❌'} ${i + 1}. ${r.matchId}`);
  });
  console.log(`\nTotal: ${results.filter(r => r.success).length}/${results.length} successful`);

  await prisma.$disconnect();
}

main().catch(console.error);
