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

dotenv.config();

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

const prisma = new PrismaClient();

const matchId = '7cnm7h7qbsq2bbaxngusojh90';

async function checkLineupData() {
  const match = await prisma.liveMatch.findUnique({
    where: { id: matchId },
  });

  if (!match) {
    console.log('❌ Match not found');
    return;
  }

  console.log('\n📊 LINEUP DATA INSPECTION');
  console.log('='.repeat(80));
  
  console.log(`\n1. lineups field:`);
  console.log(`   Type: ${typeof match.lineups}`);
  console.log(`   Is null: ${match.lineups === null}`);
  console.log(`   Content:`, JSON.stringify(match.lineups, null, 2));

  console.log(`\n2. sidelined field:`);
  console.log(`   Type: ${typeof match.sidelined}`);
  console.log(`   Is null: ${match.sidelined === null}`);
  console.log(`   Content:`, JSON.stringify(match.sidelined, null, 2));

  console.log(`\n3. odds field:`);
  console.log(`   Type: ${typeof match.odds}`);
  console.log(`   Is null: ${match.odds === null}`);
  
  // Check if it's JSON object or string
  if (match.odds) {
    const oddsStr = typeof match.odds === 'string' ? match.odds : JSON.stringify(match.odds);
    console.log(`   Length: ${oddsStr.length}`);
    console.log(`   Preview: ${oddsStr.substring(0, 200)}...`);
  }

  console.log(`\n4. refereeName:`);
  console.log(`   Value: ${match.refereeName}`);

  // Now check what AI Engine sees
  console.log('\n\n🔍 AI ENGINE PERSPECTIVE');
  console.log('='.repeat(80));
  
  // Simulate AI Engine's lineup parsing
  const lineups = match.lineups as any;
  let homePlayers: any[] = [];
  let awayPlayers: any[] = [];
  
  if (lineups && typeof lineups === 'object') {
    if (lineups.home?.xi) {
      homePlayers = lineups.home.xi;
    }
    if (lineups.away?.xi) {
      awayPlayers = lineups.away.xi;
    }
  }

  console.log(`\nHome lineup count: ${homePlayers.length}`);
  console.log(`Away lineup count: ${awayPlayers.length}`);
  console.log(`Lineup source would be: ${homePlayers.length >= 9 && awayPlayers.length >= 9 ? 'confirmed_live' : 'none/probable'}`);

  await prisma.$disconnect();
}

checkLineupData().catch(console.error);
