import express from 'express';
import cors from 'cors'
import http from 'http'
import chalk from 'chalk'
import moment from 'moment'
import path from 'path';
import { initWebSocket } from './ws.js'

import Database from "./db.js"
import AuthHandler from './auth.js';
import handlers from "./handlers.js";

// Get __dirname in ES6 environment
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

class Server {
    db;
    auth;
    UIPath = path.join(__dirname, '../ui')

    registerRoutes(router) {
        router.post('/api/location', handlers.updateLocation)
        router.post('/csv', handlers.uploadCSV)
        router.post('/login', this.login)
        router.get('/*', this.get)
        return router
    }

    authMiddleware = (req, res, next) => {
        const authHeader = req.headers.authorization;
        if (!authHeader) {
            return res.status(401).json({ error: 'Authorization token required.' });
        }

        const [scheme, token] = authHeader.split(' ');
        if (scheme !== 'Bearer' || !token) {
            return res.status(401).json({ error: 'Malformed authorization header.' })
        }

        try {
            const payload = this.auth.verify(token);
            req.user = payload;
            return next();
        } catch (err) {
            return res.status(403).json({ error: 'Invalid or expired token.' });
        }
    }

    login = async (req, res) => {
        if(this.auth.login(req, res)) {
            res.writeHead(302, { 'Location': "/" }).end()
            // res.status(200).send();
        } else {
            res.status(400).send();
        }
    }

    get = async (req, res) => {
        if(!this.auth.check(req, res)) {
            if(req.url === "/") {
                res.sendFile(path.join(this.UIPath, 'auth/auth.html'));
                return;
            } else if(req.url === "/betsyross.svg") {
                res.sendFile(path.join(this.UIPath, '_/betsyross.svg'));
                return
            } else {
                return
            }
        } else {
            let url = req.url
            if(url === "/") {
                url = "/index.html"
            }

            let filePath;
            if(url.startsWith("/_")) {
                filePath = path.join(this.UIPath, url);
            } else {
                filePath = path.join(this.UIPath, "app", url);
            }

            res.sendFile(filePath, (err) => {
                if (err) {
                    console.error(`Error serving ${filePath}:`, err);
                    res.status(err.status || 500).send('File not found or error serving file.');
                }
            });
        }
    }

    logRequest(req, res, next) {
        const formattedDate = moment().format('M.D');
        const formattedTime = moment().format('h:mma');
        if(req.url.includes("/api/")) {
            console.log(chalk.blue(`       ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
        } else {
            if(req.url === "/")
                console.log(chalk.gray(`       ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
        }
        next(); 
    }

    logResponse(req, res, next) {
        const originalSend = res.send;
        res.send = function (body) {
            if(res.statusCode >= 400) {
                console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
            } else {
                console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
            }
            originalSend.call(this, body);
        };
        next();
    }

    constructor() {
        global.db = new Database()
        this.auth = new AuthHandler()
        const app = express();
        app.use(cors({ origin: '*' }));
        app.use(express.json());

        app.use(this.logRequest);
        app.use(this.logResponse);

        let router = express.Router();
        this.registerRoutes(router)
        app.use('/', router);

        const server = http.createServer(app);
        initWebSocket(server);
        const PORT = 3008;
        server.listen(PORT, () => {
            console.log("\n")
            console.log(chalk.yellow("**************America****************"))
            console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
            console.log(chalk.yellow("***************************************"))
            console.log("\n")
        });

        process.on('SIGINT', async () => {
            console.log(chalk.red('Closing server...'));
            console.log(chalk.green('Database connection closed.'));
            process.exit(0);
        });

        Object.preventExtensions(this);
    }
}

const server = new Server()