import pkg from 'pg';
        import express from 'express';
        import bodyParser from 'body-parser';
        import { fileURLToPath } from 'url';
        import path from 'path';

        const { Pool } = pkg;

        // Set up express app
        const app = express();
        const port = 3000;

        const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Gets the current file path/directory and defines it as dirname
        app.use(express.static(path.join(__dirname, 'public'))); // Selects the directory for static files, doesn't work for SOME REASON, idk if it works now or not, too scared to change it
        console.log('Static directory:', path.join(__dirname, 'public')); // Debugs

        // Set up body parser middleware to handle form data
        app.use(bodyParser.urlencoded({ extended: true }));
        // app.use(bodyParser.json());

        // PostgreSQL database connection configuration
        const pool = new Pool({
            host: 'localhost',           // PostgreSQL server host
            user: 'postgres',        // PostgreSQL username
            password: 'password',    // PostgreSQL password
            database: 'db1',    // Database name
            port: 5432,     // Port
        });

        // Function to insert data into the PostgreSQL database
        async function insertData(name, pass, email) {
            try {
                const client = await pool.connect();

                // Prepare the SQL query to insert data
                const sql = 'INSERT INTO users (name, pass, email) VALUES ($1, $2, $3) RETURNING *';
                const values = [name, pass, email];

                // Execute the query
                const res = await client.query(sql, values);
                console.log('Data inserted:', res.rows[0]);

                client.release();  // Release the client back to the pool
            } catch (error) {
                console.error('Error inserting data into the database:', error);
            }
        }

        app.post('/submit', async (req, res) => {
            const {name, pass, email} = req.body;

            //Insert data into the db
            await insertData(name, pass, email);

            //Send respons back to user + debugging
            console.log("Data submitted successfully")
            res.send(`Thank you ${name}`)
        })

        // Start the server
        app.listen(port, () => {
            console.log(`Server running at http://localhost:${port}`);
        });

        app.get('/404', (req, res) => {
            res.send("Error 404, page not found")
        });

        // Pushes index.ejs to the  res object
        app.get('/', (req, res) => {
            res.sendFile('index.html');
        });

        app.get('/booking', (req, res) => {
            res.sendFile('C:/Users/jenso/Documents/CodeAndTing/School/TokaNew/public/booking.html');
        });

        app.get('/contact', (req, res) => {
            res.sendFile('C:/Users/jenso/Documents/CodeAndTing/School/TokaNew/public/contact.html');
        });

        app.get('/about', (req, res) => {
            res.sendFile('C:/Users/jenso/Documents/CodeAndTing/School/TokaNew/public/about.html');
        });

        app.get('/signUp', (req, res) => {
            res.sendFile('C:/Users/jenso/Documents/CodeAndTing/School/TokaNew/public/signUp.html');
        });

        // // Route to serve the booking page
        // app.get('/booking', (req, res) => {
        //     res.sendFile(path.join(express.static, 'booking.html')); // Ensure this points to the correct file
        // });