const logger = require("../../utils/logger");
const { client } = require('../../graphql-client/graphql-client');
const { INSERT_MEDIA_ANALYTICS, GET_BODYSHOP_BY_ID } = require("../../graphql-client/queries");

const documentAnalytics = async (req, res) => {
    try {
        const { data } = req.body;

        //Check if the bodyshopid is real as a "security" measure
        if (!data.bodyshopid) {
            throw new Error("No bodyshopid provided in data");
        }

        const { bodyshops_by_pk } = await client.request(GET_BODYSHOP_BY_ID, {
            id: data.bodyshopid
        });

        if (!bodyshops_by_pk) {
            throw new Error("Invalid bodyshopid provided in data");
        }

        await client.request(INSERT_MEDIA_ANALYTICS, {
            mediaObject: data
        });

        res.json({ status: "success" })
    } catch (error) {
        logger.log("document-analytics-error", "ERROR", req?.user?.email, null, {
            error: error.message,
            stack: error.stack
        });
        res.status(500).json({ error: error.message, stack: error.stack });
    }
};


exports.default = documentAnalytics;
