import express from 'express';

// Route imports
import test_route from './test.js';
import version_route from './version.js';

import user_route from './user/user.js';
import user_schema from './user/user_schema.js';

import user_codecheck_route from './user/user_codecheck.js';
import user_codecheck_schema from './user/user_codecheck_schema.js';

import products_route from './products/products.js';
import products_schema from './products/products_schema.js';

import image_route from './image/image.js';
import image_schema from './image/image_schema.js';

import transaction_route from './transaction/transaction.js';
import transaction_schema from './transaction/transaction_schema.js';

// Router base is '/api/v1'
const Router = express.Router({ strict: false });

// All empty strings are undefined (not null!) values (body)
Router.use('*', function (req, res, next) {
	for (let key in req.body) {
		if (req.body[key] === '') {
			req.body[key] = undefined;
		}
	}
	next();
});

// All empty strings are undefined (not null!) values (query)
Router.use('*', function (req, res, next) {
	for (let key in req.query) {
		if (req.query[key] === '') {
			req.query[key] = undefined;
		}
	}
	next();
});

// All api routes lowercase! Yea I know but when strict: true it matters.
Router.route('/user').get(user_route.get).post(user_route.post).patch(user_route.patch).delete(user_route.del);
Router.route('/user/describe').get(user_schema);

Router.route('/user/codecheck').get(user_codecheck_route.get);
Router.route('/user/codecheck/describe').get(user_codecheck_schema);

Router.route('/products').get(products_route.get).post(products_route.post).patch(products_route.patch).delete(products_route.del);
Router.route('/products/describe').get(products_schema);

Router.route('/image').get(image_route.get).post(image_route.post).patch(image_route.post).delete(image_route.del); // POST and PATCH are handled in 'image_route.post'
Router.route('/image/describe').get(image_schema);

Router.route('/transaction').get(transaction_route.get).post(transaction_route.post).patch(transaction_route.patch).delete(transaction_route.del);
Router.route('/transaction/describe').get(transaction_schema);

Router.route('/version').get(version_route.get);
Router.route('/test').get(test_route.get);

export default Router;
