import AbstractProductService from '../services/abstractproduct.js';
import statuses from '../utils/status.js';
import log from '../utils/log.js';
import fs from 'fs';
import path from 'path';

const TAG = "/controllers/abstractproduct.js";

class AbstractProductController {
    async create(req, res) {
        try {
            const { groupId, localId, barcode, name, net_weight, image_filename, category, unit } = req.body;

            const tempPath = req.file.path;
            const targetPath = path.join(path.resolve(path.dirname('')), `/uploads/${image_filename}.png`);

            if (path.extname(req.file.originalname).toLowerCase() !== ".png") {
                return res.status(400).send("Only .png files are allowed")
            }

            fs.renameSync(tempPath, targetPath);

            await AbstractProductService.create(groupId, localId, barcode, name, net_weight, image_filename, category, unit);
            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, barcode, name, net_weight, image_filename, category, unit } = req.body;

            const tempPath = req.file.path;
            const targetPath = path.join(path.resolve(path.dirname('')) + `/uploads/${image_filename}.png`);


            if (barcode) await AbstractProductService.updateBarcode(groupId, localId, barcode);

            if (name) await AbstractProductService.updateName(groupId, localId, name);

            if (net_weight) await AbstractProductService.updateNetWeight(groupId, localId, net_weight);

            if (image_filename && tempPath) {
                fs.renameSync(tempPath, targetPath);
                await AbstractProductService.updateImageFilename(groupId, localId, image_filename);
            } else if (image_filename && !tempPath) {
                return res.status(400).send("You must supply image file along with its hash");
            } else if (!image_file && tempPath) {
                return res.status(400).send("You must supply image file hash along with file");
            }

            if (category) await AbstractProductService.updateCategory(groupId, localId, category);

            if (unit) await AbstractProductService.updateUnit(groupId, localId, unit);

            return res.status(200).send("Successfull");
        } catch (e) {
            switch (e.status) {
                case statuses.invalid_syntax:
                    log.error(e.original);
                    return res.status(400).send(e.message);
                default:
                    log.error(e.original);
                    return res.status(500).send(e.message);
            }
        }
    }

    // async updateImage(req, res) {}
};

export default new AbstractProductController();