import express from 'express';
import api from './api';
import path from 'path';

const app = express();
const port = 3000;

declare module 'express-serve-static-core' {
    interface Request {
        isAuthenticated?: boolean;
    }
}

// Checking auth
app.use((req, res, next) => {
    req.isAuthenticated = true; // Mock authentication, replace with real logic
    // Add your authentication logic here
    next();
});

app.use('/api', api);

app.get('/', (req, res) => {
    if (!req.isAuthenticated)
        return res.status(301).redirect('/auth');
    res.status(301).redirect('/home');
});

app.get('/auth', (req, res) => {
    res.sendFile('index.html', { root: path.join(__dirname, 'static') });
});

app.get('/home', (req, res) => {
    res.sendFile('index.html', { root: path.join(__dirname, 'static') });
});

app.use(express.static(path.join(__dirname, 'static')));

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