import { Router } from 'express';

import {
	createBan,
	createTemplate,
	fetchUser,
	fetchUserTemplates,
	removeTemplate,
} from '../users.js';

async function fetchUserApi(req, res) {
	const user = await fetchUser(req.params.userId, {}, req.user);

	res.send(user);
}

async function fetchUserTemplatesApi(req, res) {
	const templates = await fetchUserTemplates(req.user);

	res.send(templates);
}

async function createTemplateApi(req, res) {
	const template = await createTemplate(req.body, req.user);

	res.send(template);
}

async function removeTemplateApi(req, res) {
	await removeTemplate(req.params.templateId, req.user);

	res.status(204).send();
}

async function createBanApi(req, res) {
	await createBan(req.body, req.user);

	res.status(204).send();
}

export const router = Router();

router.get('/api/users/:userId', fetchUserApi);
router.get('/api/users/:userId/templates', fetchUserTemplatesApi);

router.post('/api/templates', createTemplateApi);
router.delete('/api/templates/:templateId', removeTemplateApi);

router.post('/api/bans', createBanApi);
