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

const createPendingTicket = async () => {
  try {
    console.log('\n🎫 CRÉATION D\'UN TICKET DE TEST PENDING\n');

    // Get a random available ticket
    const ticketResult = await pool.query(`
      SELECT id, code, prize_id
      FROM tickets
      WHERE status IS NULL
      ORDER BY RANDOM()
      LIMIT 1
    `);

    if (ticketResult.rows.length === 0) {
      console.error('❌ Aucun ticket disponible');
      process.exit(1);
    }

    const ticket = ticketResult.rows[0];

    // Get client (Jean Dupuis)
    const clientResult = await pool.query(`
      SELECT id, email, first_name, last_name
      FROM users
      WHERE email = 'client1@example.com'
    `);

    if (clientResult.rows.length === 0) {
      console.error('❌ Client non trouvé');
      process.exit(1);
    }

    const client = clientResult.rows[0];

    // Update ticket to PENDING
    await pool.query(`
      UPDATE tickets
      SET status = 'PENDING', user_id = $1, played_at = NOW()
      WHERE id = $2
    `, [client.id, ticket.id]);

    console.log('✅ Ticket de test créé avec succès!\n');
    console.log('📋 DÉTAILS:');
    console.log(`   Code: ${ticket.code}`);
    console.log(`   Client: ${client.first_name} ${client.last_name} (${client.email})`);
    console.log(`   Statut: PENDING`);
    console.log('\n🧪 Vous pouvez maintenant tester:');
    console.log('   1. Connexion employé: employee1@thetiptop.com / Employee123!');
    console.log('   2. Page: /employe/verification');
    console.log(`   3. Le ticket ${ticket.code} devrait apparaître dans la liste`);

    process.exit(0);
  } catch (error) {
    console.error('❌ Erreur:', error.message);
    console.error(error.stack);
    process.exit(1);
  }
};

createPendingTicket();
