const { logger } = require("nodejs-log-agent");
const { sendError } = require("../libs/errorCapture");
const db = require("../models");
const { body, validationResult } = require("express-validator");
const createValidate = [
    body('title').notEmpty().withMessage('title cannot be empty'),
    body('grup').notEmpty().withMessage('Grup cannot be empty'),
    body('value').notEmpty().withMessage('Value Id cannot be empty'),
]

const create = async (req, res) => {
    const error = validationResult(req)
    if (!error.isEmpty()) {
        logger.error("validation error on create Template", { message : error.message , stack : error.stack, error : error });
        return res.json({ 
            status : false,
            message: 'validation error',
            error: error.array() 
        })
    }

    try {
        const template = await db.reference.upsert(req.body)
        return res.json({
            status :true,
            message : "Save data Successfully",
            data : template
        });
    } catch (error) {
        logger.error("function create Template", { message : error.message , stack : error.stack, error : error });
        sendError(error.message)
        res.json({
            status: false,
            message: error.message,
            error
        });
    }
}
const get = async (req, res) => {
    try {
        const templates = await db.reference.findAll({
            where : {
                grup : "invoice"
            }
        });
        return res.json({
            status :true,
            message : "success",
            data : templates
        });
    } catch (error) {
        logger.error("function get Templates", { message : error.message , stack : error.stack, error : error });
        sendError(error.message)
        res.json({
            status: false,
            message: error.message,
            error
        });
    }
}

const update = async (req, res) => {
    try {
        const template = await db.reference.update({
            value : req.body.value,
            title : req.body.title
        }, {
            where : {
                id : req.body.id
            }
        })
        return res.json({
            status :true,
            message : "Update Successfully",
            data : template
        });
    } catch (error) {
        logger.error("function update Template", { message : error.message , stack : error.stack, error : error });
        sendError(error.message)
        res.json({
            status: false,
            message: error.message,
            error
        });
    }
}

const destroy = async (req, res) => {
    const { id } = req.params
    try {
        const template = await db.reference.destroy({
            where : {
                id : id
            }
        })
        return res.json({
            status :true,
            message : "delete Successfully",
            data : template
        });
    } catch (error) {
        logger.error("function delete Template", { message : error.message , stack : error.stack, error : error });
        sendError(error.message)
        res.json({
            status: false,
            message: error.message,
            error
        });
    }
}
module.exports = {
    get,
    update,
    create,
    createValidate,
    destroy
}