// pages/api/readLocations.js
import mysql from "mysql";

const dbConfig = {
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  port: process.env.DB_PORT,
};
console.log("my dbconfig: ", dbConfig);
export default function handler(req, res) {
  const connection = mysql.createConnection(dbConfig);

  connection.connect((err) => {
    if (err) {
      console.error("Fehler beim Verbinden:", err.stack);
      res.status(500).json({ error: "Verbindungsfehler zur Datenbank" });
      return;
    }

    console.log("Verbunden als ID", connection.threadId);

    connection.query(
      "SELECT idPoi, description, idPoiTyp, ST_AsText(position) AS position FROM poi",
      (error, results) => {
        if (error) {
          console.error("Fehler beim Abrufen der API", error);
          res.status(500).json({ error: "Fehler bei der Abfrage" });
          return;
        }

        // Wichtig: Senden Sie die Antwort zurück
        res.status(200).json(results);
        console.log(
          "--------------- location.js  ---------------",
          "results in location.js : ",
          results,
          "---------------------- location.js end ---------------------------"
        );
        connection.end();
      }
    );
  });
}
