import analyze from './preprocessing/nlp.js'
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import path from 'path';
import { fileURLToPath } from 'url';

const __dirname = fileURLToPath(import.meta.url);

const port = 3000
const app = express();

// Serve the frontend files as static files
app.use(express.static(path.join(__dirname, '..', 'static')));

app.use(express.json());
app.use(morgan('combined'));
app.use(cors());
app.options('*', cors());

app.post('/api', (req, res) => {
    try {
        let response = analyze(req.body.text);
        res.send(response);
    } catch (error) {
        res.status(400).send({ error: error.message });
    }
})

app.get('*', (req, res) => {
    console.log('GET request received');
    res.sendFile(path.join(__dirname, '..', 'static', 'index.html'));
  });


app.listen(port, () => {
    console.log(`App listening at http://localhost:${port}`)
})