import { NextRequest, NextResponse } from 'next/server';

export async function GET(request: NextRequest) {
    try {
        console.log('🧪 API Test Backend: Début du test...');
        
        // Test de connexion au backend
        const backendUrl = 'http://localhost:8080';
        
        // Test 1: Health check
        console.log('🧪 Test 1: Health check...');
        const healthResponse = await fetch(`${backendUrl}/q/health`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        
        const healthData = await healthResponse.text();
        console.log('🧪 Health check response:', healthResponse.status, healthData);
        
        // Test 2: API Chantiers
        console.log('🧪 Test 2: API Chantiers...');
        const chantiersResponse = await fetch(`${backendUrl}/api/v1/chantiers`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        
        const chantiersData = await chantiersResponse.text();
        console.log('🧪 Chantiers response:', chantiersResponse.status, chantiersData);
        
        // Test 3: API Clients
        console.log('🧪 Test 3: API Clients...');
        const clientsResponse = await fetch(`${backendUrl}/api/clients`, {
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
            },
        });
        
        const clientsData = await clientsResponse.text();
        console.log('🧪 Clients response:', clientsResponse.status, clientsData);
        
        // Résumé des tests
        const results = {
            health: {
                status: healthResponse.status,
                data: healthData
            },
            chantiers: {
                status: chantiersResponse.status,
                data: chantiersData
            },
            clients: {
                status: clientsResponse.status,
                data: clientsData
            }
        };
        
        console.log('🧪 Résultats complets:', results);
        
        return NextResponse.json({
            success: true,
            message: 'Tests backend terminés',
            results
        });
        
    } catch (error) {
        console.error('🧪 Erreur test backend:', error);
        return NextResponse.json({
            success: false,
            error: error instanceof Error ? error.message : 'Erreur inconnue'
        }, { status: 500 });
    }
}
