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 matchIds = [
  '7cnm7h7qbsq2bbaxngusojh90',
  '7lmrfu2k1e2uxprxfxgaevcb8',
  '3ko3otchy41d28rzxfpvl3d3o'
];

async function getMatches() {
  for (const matchId of matchIds) {
    try {
      const match = await prisma.liveMatch.findUnique({
        where: { id: matchId },
        include: {
          homeTeam: true,
          awayTeam: true,
          league: true,
        },
      });

      if (!match) {
        console.log(`\n❌ Maç bulunamadı: ${matchId}`);
        continue;
      }

      console.log(`\n${'='.repeat(80)}`);
      console.log(`🏟️ ${match.homeTeam?.name} vs ${match.awayTeam?.name}`);
      console.log(`📍 Lig: ${match.league?.name}`);
      console.log(`📅 Maç Zamanı: ${new Date(Number(match.mstUtc)).toLocaleString('tr-TR')}`);
      console.log(`👨‍⚖️ Hakem: ${match.refereeName || 'Bilinmiyor'}`);
      console.log('='.repeat(80));
      
      // Lineups
      if (match.lineups) {
        const lineups = match.lineups as any;
        
        // Home team
        if (lineups.home && lineups.home.xi) {
          console.log(`\n🏠 EV SAHİBİ İLK 11 (${match.homeTeam?.name}):`);
          console.log('-'.repeat(80));
          const goalscorers = lineups.home.xi.filter((p: any) => p.events?.some((e: any) => e.name === 'goal'));
          const cards = lineups.home.xi.filter((p: any) => p.events?.some((e: any) => e.name === 'yellow-card' || e.name === 'red-card'));
          const subs = lineups.home.xi.filter((p: any) => p.events?.some((e: any) => e.name === 'sub-off'));
          
          lineups.home.xi.forEach((p: any) => {
            const hasGoal = p.events?.some((e: any) => e.name === 'goal');
            const hasCard = p.events?.some((e: any) => e.name === 'yellow-card' || e.name === 'red-card');
            const marker = hasGoal ? ' ⚽' : hasCard ? ' 🟨' : '';
            console.log(`   ${p.matchName} (${p.shirtNumber}) - ${p.position}${marker}`);
          });
          
          if (goalscorers.length > 0) {
            console.log(`   ⚽ Gol Edenler: ${goalscorers.map((p: any) => p.matchName).join(', ')}`);
          }
        }
        
        // Away team
        if (lineups.away && lineups.away.xi) {
          console.log(`\n✈️ DEPLASMAN İLK 11 (${match.awayTeam?.name}):`);
          console.log('-'.repeat(80));
          lineups.away.xi.forEach((p: any) => {
            const hasGoal = p.events?.some((e: any) => e.name === 'goal');
            const hasCard = p.events?.some((e: any) => e.name === 'yellow-card' || e.name === 'red-card');
            const marker = hasGoal ? ' ⚽' : hasCard ? ' 🟨' : '';
            console.log(`   ${p.matchName} (${p.shirtNumber}) - ${p.position}${marker}`);
          });
        }
        
        // Sidelined
        if (match.sidelined) {
          const sidelined = match.sidelined as any;
          const homeSidelined = sidelined.homeTeam?.totalSidelined || 0;
          const awaySidelined = sidelined.awayTeam?.totalSidelined || 0;
          console.log(`\n🏥 Sakat/Cezalı:`);
          console.log(`   Ev Sahibi: ${homeSidelined} oyuncu`);
          console.log(`   Deplasman: ${awaySidelined} oyuncu`);
        }
      }
      
      console.log('\n');
      
    } catch (error) {
      console.error(`❌ Hata (${matchId}):`, error);
    }
  }
  
  await prisma.$disconnect();
}

getMatches();
