/**
 * Simple API test without external dependencies
 */

import http from 'http';

function testAPI() {
  console.log('🧪 Testing API endpoints...\n');

  // Test 1: Check if server is running
  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/',
    method: 'GET'
  };

  const req = http.request(options, (res) => {
    console.log('✅ Server is running on port 5000');
    console.log('Status:', res.statusCode);
    
    let data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    
    res.on('end', () => {
      console.log('Response:', data);
      
      // Test 2: Test dashboard cards endpoint
      testDashboardCards();
    });
  });

  req.on('error', (err) => {
    console.error('❌ Server is not running:', err.message);
    console.log('Please start the server with: npm start');
  });

  req.end();
}

function testDashboardCards() {
  console.log('\n📊 Testing Dashboard Cards API...');
  
  const options = {
    hostname: 'localhost',
    port: 5000,
    path: '/api/surveys/project/1/dashboard/cards',
    method: 'GET',
    headers: {
      'Authorization': 'Bearer test-token',
      'Content-Type': 'application/json'
    }
  };

  const req = http.request(options, (res) => {
    console.log('Cards API Status:', res.statusCode);
    console.log('Headers:', res.headers);
    
    let data = '';
    res.on('data', (chunk) => {
      data += chunk;
    });
    
    res.on('end', () => {
      if (res.statusCode === 200) {
        try {
          const jsonData = JSON.parse(data);
          console.log('✅ Cards API Success:', JSON.stringify(jsonData, null, 2));
        } catch (error) {
          console.log('❌ Invalid JSON response:', data.substring(0, 200));
        }
      } else {
        console.log('❌ Cards API Error:', res.statusCode, data.substring(0, 200));
      }
    });
  });

  req.on('error', (err) => {
    console.error('❌ Request error:', err.message);
  });

  req.end();
}

// Run the test
testAPI();
