import express from 'express';
import path from 'node:path';
import __path from '../handlers/path.js';
import log from '../handlers/log.js';

// Route imports
import frontend_router from './frontend/index.js';
import api_router from './api/index.js';

const Router = express.Router({ strict: false });

// static / libs routes
Router.use('/static', express.static(__path + '/static'));
Router.use('/libs/bulma', express.static(path.join(__path, 'node_modules', 'bulma', 'css'))); // /libs/bulma/bulma.css
Router.use('/libs/jquery', express.static(path.join(__path, 'node_modules', 'jquery', 'dist')));
Router.use('/libs/bootstrap-icons', express.static(path.join(__path, 'node_modules', 'bootstrap-icons')));

// Other routers
Router.use('/api', api_router);
Router.use('/', frontend_router);

// Default route.
Router.all('*', function (req, res) {
	// TODO: Respond based on content-type (with req.is('application/json'))
	if (req.is('application/json')) {
		res.status(404).json({ status: 'ERROR', errorcode: 'NOT_FOUND', message: 'Could not find specified page' });
	} else {
		res.status(404).render('errors/404', { url: req.originalUrl });
	}
});

export default Router;
