// Direct API test to check if our endpoint responds properly
// Save this as test-route.js and run it: node test-route.js

const http = require('http');
const { exec } = require('child_process');

console.log('Testing API endpoint directly...');

// Check if dev server is running
exec('lsof -i :3000', (error, stdout, stderr) => {
  if (error) {
    console.log('Development server does not appear to be running on port 3000');
    console.log('Starting development server...');
    exec('npm run dev', { cwd: '/home/sparky/Projekte/My-Weekly-ToDo-List' }, (error, stdout, stderr) => {
      if (error) {
        console.error('Failed to start server:', error);
      } else {
        console.log('Server started successfully');
      }
    });
  } else {
    console.log('Development server is running on port 3000');
  }
  
  // Try to directly test the endpoint after a short delay
  setTimeout(() => {
    console.log('Testing the signup API endpoint directly...');
    
    const postData = JSON.stringify({
      email: 'test@example.com',
      password: 'testpass123'
    });
    
    const options = {
      hostname: 'localhost',
      port: 3000,
      path: '/api/auth/signup',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
      }
    };
    
    const req = http.request(options, (res) => {
      console.log(`STATUS: ${res.statusCode}`);
      console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
      
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        console.log('RESPONSE DATA:');
        console.log(data.substring(0, 500));
        
        try {
          const jsonData = JSON.parse(data);
          console.log('✓ Parsed JSON successfully:', jsonData);
        } catch (e) {
          console.log('✗ Failed to parse JSON - got HTML or other content');
        }
      });
    });
    
    req.on('error', (e) => {
      console.error('Problem with request:', e.message);
    });
    
    req.write(postData);
    req.end();
  }, 2000);
});