// Test users API endpoint
// Using built-in fetch (Node.js 18+)

async function testUsersAPIEndpoint() {
  console.log('🧪 TESTING USERS API ENDPOINT');
  console.log('============================');
  console.log('');

  const BASE_URL = 'http://localhost:5000/api';
  
  try {
    // Test 1: Check if server is running
    console.log('1. Checking if server is running...');
    try {
      const healthResponse = await fetch(`${BASE_URL}/health`);
      if (healthResponse.ok) {
        const healthData = await healthResponse.json();
        console.log(`   ✅ Server is running: ${healthData.message}`);
      } else {
        console.log(`   ❌ Server health check failed: ${healthResponse.status}`);
      }
    } catch (error) {
      console.log(`   ❌ Server is not running: ${error.message}`);
      console.log('   💡 Please start the backend server with: npm run dev');
      return;
    }
    console.log('');

    // Test 2: Test users endpoint without authentication
    console.log('2. Testing users endpoint without authentication...');
    try {
      const usersResponse = await fetch(`${BASE_URL}/users`);
      if (usersResponse.status === 401) {
        console.log('   ✅ Authentication required (expected)');
      } else {
        console.log(`   ⚠️ Unexpected response: ${usersResponse.status}`);
      }
    } catch (error) {
      console.log(`   ❌ Error: ${error.message}`);
    }
    console.log('');

    // Test 3: Test with mock token
    console.log('3. Testing users endpoint with mock token...');
    try {
      const mockToken = 'mock-token-for-testing';
      const usersResponse = await fetch(`${BASE_URL}/users`, {
        headers: {
          'Authorization': `Bearer ${mockToken}`,
          'Content-Type': 'application/json'
        }
      });
      
      if (usersResponse.status === 401) {
        console.log('   ✅ Invalid token rejected (expected)');
      } else {
        console.log(`   ⚠️ Unexpected response: ${usersResponse.status}`);
      }
    } catch (error) {
      console.log(`   ❌ Error: ${error.message}`);
    }
    console.log('');

    console.log('============================');
    console.log('✅ USERS API ENDPOINT TEST COMPLETE');
    console.log('');
    console.log('📋 SUMMARY:');
    console.log('   - Server status: Checked');
    console.log('   - Authentication: Required');
    console.log('   - Endpoint: /api/users');
    console.log('');
    console.log('💡 NEXT STEPS:');
    console.log('   1. Make sure backend server is running: npm run dev');
    console.log('   2. Login through frontend to get valid token');
    console.log('   3. Check browser network tab for API calls');
    console.log('   4. Verify token is being sent in Authorization header');

  } catch (error) {
    console.error('❌ Error testing users API endpoint:', error);
  }
}

testUsersAPIEndpoint();
