import ProductService from '../services/product.js';
import statuses from '../utils/status.js';
import log from '../utils/log.js';

const TAG = "/controllers/product.js"

class AbstractProductController {
    async create(req, res) {
        try {
            const { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;
            await ProductService.create(groupId, localId, abstract_product_id, amount, date_of_production, expiry_date);
            return res.status(200).send("Successfull");
        } catch (e) {
            switch (e.status) {
                case statuses.duplicate:
                    return res.status(400).send(e.message);
                default:
                    log.error(e.original)
                    return res.status(500).send(e.message)
            }
        }
    }


    async update(req, res) {
        try {
            let { groupId, localId, abstract_product_id, amount, date_of_production, expiry_date } = req.body;

            if (abstract_product_id) await ProductService.updateAbstractProductId(groupId, localId, abstract_product_id);

            if (amount) await ProductService.updateAmount(groupId, localId, amount);

            if (date_of_production) await ProductService.updateDateOfProduction(groupId, localId, date_of_production);

            if (expiry_date) await ProductService.updateExpiryDate(groupId, localId, expiry_date);
        } catch (e) {
            switch (e.status) {
                case statuses.invalid_syntax:
                    log.error(e.original);
                    return res.status(400).send(e.message);
            }
        }

        return res.status(200).send("Successfull");
    }
};

export default new AbstractProductController();