const { Client } = require('pg');

async function test_insert() {
    require('dotenv').config({ path: '../.env' });
const client = new Client({
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME
});
    
    try {
        await client.connect();
        console.log("Connected to DB.");
        
        const projekt_id = "BWSamern-Ohne-Test";
        const lat = 52.3;
        const lng = 7.1;
        
        await client.query('BEGIN');
        await client.query('DELETE FROM geodaten.wea_standorte WHERE projekt_id = $1', [projekt_id]);
        
        console.log("Inserting test row...");
        await client.query(
            `INSERT INTO geodaten.wea_standorte (
                wea_nummer, anlagentyp, nabenhoehe, rotordurchmesser, projekt_id, geom
            ) VALUES ($1, $2, $3, $4, $5, ST_Transform(ST_SetSRID(ST_MakePoint($6, $7), 4326), 25832))`,
            ['TEST-1', 'Vestas V162', 169, 162, projekt_id, lng, lat]
        );
        
        await client.query('COMMIT');
        console.log("Insert successful!");
        
        const res = await client.query('SELECT *, ST_AsText(ST_Transform(geom, 4326)) as geom_wgs84 FROM geodaten.wea_standorte WHERE projekt_id = $1', [projekt_id]);
        console.log("Verified entry:", JSON.stringify(res.rows, null, 2));
        
    } catch (e) {
        console.error("Test failed:", e);
    } finally {
        await client.end();
    }
}
test_insert();
