const db = require('./config/database');

async function testExactScenario() {
  try {
    console.log('Testing the exact scenario reported by user...');
    
    // The user reports getting [{"id":1}, {"id":51,"name":1}]
    // Let's see if we can reproduce this
    
    // First, let's check if there's any child with ID 51
    const [child51] = await db.execute('SELECT * FROM children WHERE id = 51');
    console.log('Child with ID 51:', child51[0]);
    
    // Let's also check all children to see if any have problematic data
    const [allChildren] = await db.execute('SELECT id, first_name, last_name FROM children ORDER BY id');
    console.log('\nAll children:');
    allChildren.forEach(child => {
      console.log(`ID: ${child.id}, first_name: "${child.first_name}", last_name: "${child.last_name}"`);
      
      // Check if concatenating could produce "1"
      const name = `${child.first_name} ${child.last_name}`;
      if (name.trim() === '1' || name.trim() === '1 ') {
        console.log(`  *** FOUND: Child ID ${child.id} produces name "${name}"`);
      }
    });
    
    // Let's test what would happen if we had a child with first_name = "1" and last_name = null
    console.log('\nTesting edge case:');
    const testChild = { id: 51, first_name: '1', last_name: null };
    const testName = `${testChild.first_name} ${testChild.last_name}`;
    console.log(`Test child result:`, { id: testChild.id, name: testName });
    console.log('Name type:', typeof testName);
    
    // What if we somehow had just the number 1 as first_name?
    const testChild2 = { id: 51, first_name: 1, last_name: null };
    const testName2 = `${testChild2.first_name} ${testChild2.last_name}`;
    console.log(`Test child2 result:`, { id: testChild2.id, name: testName2 });
    console.log('Name2 type:', typeof testName2);
    
    // Let's also check if there might be some API endpoint that returns a different structure
    console.log('\nChecking for any API that might return just {id, name}...');
    
    // Maybe there's an endpoint that returns children with just id and name?
    // Let's simulate what would happen if we had a query like:
    const [simpleQuery] = await db.execute(`
      SELECT id, first_name as name FROM children WHERE parent_id = ? LIMIT 2
    `, [4]);
    console.log('Simple query result:', simpleQuery);
    
    // What if there's a case where first_name is literally the number 1?
    console.log('\nLooking for any numeric values in name fields:');
    const [numericNames] = await db.execute(`
      SELECT id, first_name, last_name,
             CAST(first_name AS UNSIGNED) as first_name_num,
             CAST(last_name AS UNSIGNED) as last_name_num
      FROM children 
      WHERE first_name REGEXP '^[0-9]+$' OR last_name REGEXP '^[0-9]+$'
    `);
    console.log('Numeric names found:', numericNames);
    
    process.exit(0);
  } catch (error) {
    console.error('Error:', error.message);
    process.exit(1);
  }
}

testExactScenario();
