const https = require('https');

const lat = 51.4332;
const lng = 7.6616;
const size = 0.05;
const bbox = `${lat - size},${lng - size},${lat + size},${lng + size},urn:ogc:def:crs:EPSG::4326`;
const url = `https://www.wfs.nrw.de/umwelt/erneuerbare_energien_wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetFeature&TYPENAMES=erneuerbare_energien_wfs:WFS_Windenergie&BBOX=${bbox}&COUNT=10`;

https.get(url, (res) => {
    let data = '';
    res.on('data', (chunk) => data += chunk);
    res.on('end', () => {
        console.log("Response length:", data.length);
        const features = [];
        const blocks = data.split(/<wfs:member[^>]*>/);
        console.log("Found blocks:", blocks.length - 1);

        for (let i = 1; i < blocks.length; i++) {
            const block = blocks[i];
            const posMatch = block.match(/<gml:pos[^>]*>(.*?)<\/gml:pos>/);
            const ibjahrMatch = block.match(/<erneuerbare_energien_wfs:ibjahr[^>]*>(.*?)<\/erneuerbare_energien_wfs:ibjahr>/);

            if (posMatch) {
                const coords = posMatch[1].trim().split(/\s+/).map(Number);
                features.push({
                    coords: coords,
                    ibjahr: ibjahrMatch ? ibjahrMatch[1].trim() : "Unbekannt"
                });
            }
        }
        console.log("Parsed features:", features.length);
        if (features.length > 0) console.log("First feature:", features[0]);
    });
}).on('error', (e) => {
    console.error(e);
});
