const mysql = require('mysql2/promise');
require('dotenv').config();

async function debugStudents() {
  const connection = await mysql.createConnection({
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_NAME || 'sts_v2'
  });

  try {
    console.log('🔍 Debugging students API...');
    
    // Check what the API query returns
    const query = `
      SELECT c.id, c.first_name, c.last_name, c.standard, c.division, c.roll_number,
             d.id as device_id, d.type as device_type, d.status as device_status,
             d.battery_level, d.last_update
      FROM children c
      LEFT JOIN devices d ON c.device_id = d.id
      WHERE c.school_id = ?
      ORDER BY c.standard, c.division, c.roll_number
    `;
    
    const [rows] = await connection.execute(query, [1]);
    console.log(`📋 Found ${rows.length} students for school_id = 1:`);
    
    rows.forEach(student => {
      console.log(`  - ${student.first_name} ${student.last_name} (${student.standard}-${student.division}) - Device: ${student.device_id}`);
    });

    console.log('\n📱 Raw API response would be:');
    console.log(JSON.stringify(rows, null, 2));

  } catch (error) {
    console.error('Error:', error);
  } finally {
    await connection.end();
  }
}

debugStudents();