import { pool } from '../db.js';

async function verifyTickets() {
  try {
    console.log('\n📊 VÉRIFICATION DES TICKETS GÉNÉRÉS\n');

    // Compter total et vérifier les longueurs de code
    const statsResult = await pool.query(`
      SELECT
        COUNT(*) as total,
        MIN(LENGTH(code)) as min_len,
        MAX(LENGTH(code)) as max_len
      FROM tickets
    `);

    console.log('✅ Statistiques générales:');
    console.log(`   Total de tickets: ${parseInt(statsResult.rows[0].total).toLocaleString('fr-FR')}`);
    console.log(`   Longueur minimale des codes: ${statsResult.rows[0].min_len} caractères`);
    console.log(`   Longueur maximale des codes: ${statsResult.rows[0].max_len} caractères`);

    // Distribution par lot
    const distResult = await pool.query(`
      SELECT
        p.name,
        p.type,
        COUNT(t.id) as count,
        ROUND((COUNT(t.id)::numeric / (SELECT COUNT(*) FROM tickets) * 100), 2) as percentage
      FROM prizes p
      LEFT JOIN tickets t ON p.id = t.prize_id
      GROUP BY p.id, p.name, p.type
      ORDER BY count DESC
    `);

    console.log('\n📦 Distribution par lot:');
    distResult.rows.forEach(p => {
      const count = parseInt(p.count).toLocaleString('fr-FR');
      const percent = p.percentage || '0';
      console.log(`   • ${p.name}: ${count} tickets (${percent}%)`);
    });

    // Exemples de codes
    const samplesResult = await pool.query(`
      SELECT code FROM tickets ORDER BY code LIMIT 5
    `);

    console.log('\n🎫 Exemples de codes générés:');
    samplesResult.rows.forEach(t => {
      console.log(`   • ${t.code}`);
    });

    console.log('\n✅ Vérification terminée!\n');
    await pool.end();
    process.exit(0);
  } catch (error) {
    console.error('❌ Erreur:', error.message);
    await pool.end();
    process.exit(1);
  }
}

verifyTickets();
