const http = require('http');

function testLogin(endpoint, email, password, userType) {
  return new Promise((resolve) => {
    const postData = JSON.stringify({
      email: email,
      password: Buffer.from(password).toString('base64')
    });

    const options = {
      hostname: 'localhost',
      port: 3002,
      path: endpoint,
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
      }
    };

    console.log(`\n=== Testing ${userType} Login ===`);
    console.log(`Endpoint: ${endpoint}`);
    console.log(`Email: ${email}`);
    console.log(`Password: ${password}`);

    const req = http.request(options, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        console.log(`Status: ${res.statusCode}`);
        if (res.statusCode === 200) {
          const response = JSON.parse(data);
          console.log(`✓ SUCCESS - User: ${response.user.name}`);
        } else {
          console.log(`✗ FAILED - ${data}`);
        }
        resolve();
      });
    });

    req.on('error', (error) => {
      console.error('Error:', error.message);
      resolve();
    });

    req.write(postData);
    req.end();
  });
}

async function runTests() {
  // Test correct combinations
  await testLogin('/api/auth/school/login', 'school@test.com', 'school123', 'School');
  await testLogin('/api/auth/parent/login', 'parent@email.com', 'password123', 'Parent');
  await testLogin('/api/auth/admin/login', 'admin@gpstracking.com', 'admin123', 'Admin');
  
  console.log('\n=== WRONG COMBINATIONS (should fail) ===');
  // Test wrong combinations
  await testLogin('/api/auth/school/login', 'parent@email.com', 'password123', 'Parent on School endpoint');
}

runTests();