// Simple test script to verify API connectivity
const http = require('http');

const API_URL = 'http://localhost:3001';

function makeRequest(path) {
    return new Promise((resolve, reject) => {
        http.get(`${API_URL}${path}`, (res) => {
            let data = '';
            res.on('data', (chunk) => data += chunk);
            res.on('end', () => {
                try {
                    resolve({ status: res.statusCode, data: JSON.parse(data) });
                } catch (e) {
                    resolve({ status: res.statusCode, data: data });
                }
            });
        }).on('error', reject);
    });
}

async function runTests() {
    console.log('Testing SAMI-CLOUD API...\n');

    // Test 1: Health Check
    console.log('1. Testing health endpoint...');
    try {
        const health = await makeRequest('/health');
        if (health.status === 200) {
            console.log('   ✓ Health check passed');
        } else {
            console.log('   ✗ Health check failed:', health.status);
        }
    } catch (error) {
        console.log('   ✗ Health check error:', error.message);
    }

    // Test 2: API Test Endpoint
    console.log('\n2. Testing API test endpoint...');
    try {
        const test = await makeRequest('/api/v1/caddy/test');
        if (test.status === 200) {
            console.log('   ✓ API test passed');
            console.log('   Platform:', test.data.platform);
            console.log('   Caddy Admin API:', test.data.caddyAdminApi);
            console.log('   DNS Server API:', test.data.dnsServerApi);
            console.log('   DNS Token:', test.data.dnsTokenConfigured ? 'Configured' : 'Not configured');
        } else {
            console.log('   ✗ API test failed:', test.status);
        }
    } catch (error) {
        console.log('   ✗ API test error:', error.message);
    }

    // Test 3: Services Endpoint
    console.log('\n3. Testing services endpoint...');
    try {
        const services = await makeRequest('/api/v1/services');
        if (services.status === 200) {
            console.log('   ✓ Services endpoint passed');
            console.log('   Found', services.data.services.length, 'services');
        } else {
            console.log('   ✗ Services endpoint failed:', services.status);
        }
    } catch (error) {
        console.log('   ✗ Services endpoint error:', error.message);
    }

    console.log('\nTests complete!');
}

runTests().catch(console.error);
