import UserService from '../services/user.js';
import GroupService from '../services/group.js';
import AbstractProductService from '../services/abstractproduct.js';
import ProductService from '../services/product.js';
import CategoryService from '../services/category.js';
import log from '../utils/log.js';
import statuses from '../utils/status.js';

const TAG = "/middlewares/existance.js";

const usernameExists = async (req, res, next) => {
    try {
        let { username } = req.body;

        let user = await UserService.getByUsername(username);
        if (!user || user == statuses.not_found) return res.status(404).send("User not found");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameExists: ${e}`)); }
};

const usernameDoesntExist = async (req, res, next) => {
    try {
        let { username } = req.body;

        let user = await UserService.getByUsername(username);
        if (user != undefined && user != statuses.not_found) return res.status(400).send("Such username already taken");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameDoesntExist: ${e}`)); }
};

const groupExists = async (req, res, next) => {
    try {
        let groupId = req.params.groupId || req.body.groupId;

        let group = await GroupService.getById(groupId);

        if (!group || group == statuses.not_found) return res.status(404).send("Group not found");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupExists: ${e}`)) }
};

const groupDoesntExist = async (req, res, next) => {
    try {
        let groupId = req.params.groupId || req.body.groupId;

        let group = await GroupService.getById(groupId);

        if (group || group != statuses.not_found) return res.status(400).send("Such group already exists");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupDoesntExist: ${e}`)) }
};

const groupNameDoesntExist = async (req, res, next) => {
    try {
        const { groupName } = req.params;

        let group = await GroupService.getByName(groupName);
        if (group) return res.status(400).send("Such group name already exists");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupNameDoesntExist: ${e}`)); }
};

const abstractProductExists = async (req, res, next) => {
    try {
        const { groupId, localId } = req.body;

        let result = await AbstractProductService.exists(groupId, localId);
        if (!result) return res.status(404).send("Abstract product not found");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/abstractProductExists: ${e}`)); }
};

const productExists = async (req, res, next) => {
    try {
        const { groupId, localId } = req.body;

        let result = await ProductService.exists(groupId, localId);
        if (!result) return res.status(404).send("Product not found");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/productExists: ${e}`)); }
};

const categoryNameDoesntExist = async (req, res, next) => {
    try {
        const { categoryName, localId, groupId } = req.body;

        let result = await CategoryService.getByName(groupId, localId, categoryName);
        if (result != statuses.not_found) return res.status(400).send("Such category name exists");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/categoryNameDoesntExist: ${e}`)); }
};

const categoryExists = async (req, res, next) => {
    try {
        const { localId, groupId } = req.body;

        let result = await CategoryService.getById(groupId, localId);
        if (!result || result == statuses.not_found) return res.status(404).send("No such category");
        next();
    } catch (e) { return res.status(500).send(log.unknownError(`${TAG}/categoryExists: ${e}`)); }
};

export default {
    usernameExists,
    usernameDoesntExist,

    groupExists,
    groupDoesntExist,
    groupNameDoesntExist,

    abstractProductExists,
    productExists,

    categoryNameDoesntExist,
    categoryExists
};