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

async function addStudentsOnly() {
  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('Adding students to existing database...');

    // Add students to the children table for school_id = 1
    await connection.execute(`
      INSERT IGNORE INTO children (id, parent_id, first_name, last_name, standard, division, roll_number, school_id, device_id, status) VALUES 
      (1, 4, 'Emma', 'Johnson', '5th', 'A', 15, 1, 'DEV001', 'active'),
      (2, 4, 'Alex', 'Johnson', '3rd', 'B', 22, 1, 'DEV002', 'active'),
      (3, 4, 'Sarah', 'Smith', '7th', 'A', 8, 1, 'DEV003', 'active')
    `);

    // Add some location data
    await connection.execute(`
      INSERT IGNORE INTO device_locations (device_id, latitude, longitude, address, timestamp) VALUES 
      ('DEV001', 28.6139, 77.2090, 'Near School Gate, Delhi', NOW() - INTERVAL 5 MINUTE),
      ('DEV002', 28.6129, 77.2080, 'School Playground, Delhi', NOW() - INTERVAL 3 MINUTE),
      ('DEV003', 28.6149, 77.2100, 'Library Building, Delhi', NOW() - INTERVAL 2 MINUTE)
    `);

    console.log('✅ Students added successfully!');
    console.log('📋 Added 3 students: Emma, Alex, Sarah');
    console.log('📍 Added location data for all devices');

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

addStudentsOnly();