const logger = require("../utils/logger");
const client = require("../graphql-client/graphql-client").client;

exports.createAssociation = async (req, res) => {
  logger.log("admin-create-association", "debug", req.user.email, null, {
    request: req.body,
    ioadmin: true
  });
  const { shopid, authlevel, useremail } = req.body;

  const result = await client.request(
    `mutation INSERT_ASSOCIATION($assoc: associations_insert_input!){
  insert_associations_one(object:$assoc){
    id
    authlevel
    useremail
    active
  }
}`,
    {
      assoc: { shopid, authlevel, useremail, active: false }
    }
  );
  res.json(result);
};
exports.createShop = async (req, res) => {
  logger.log("admin-create-shop", "debug", req.user.email, null, {
    request: req.body,
    ioadmin: true
  });
  const { bodyshop, ronum } = req.body;

  try {
    const result = await client.request(
      `mutation INSERT_BODYSHOPS($bs: bodyshops_insert_input!) {
        insert_bodyshops_one(object: $bs) {
          id
          vendors {
            id
          }
        }
      }`,
      {
        bs: {
          ...bodyshop,
          counters: {
            data: [
              { countertype: "ronum", count: ronum },
              { countertype: "ihbnum", count: 1 },
              { countertype: "paymentnum", count: 1 }
            ]
          },
          vendors: {
            data: [{ name: "In-House" }]
          }
        }
      }
    );
    const bodyshopId = result.insert_bodyshops_one.id;
    const vendorId = result.insert_bodyshops_one.vendors[0].id;

    if (!bodyshopId || !vendorId) {
      throw new Error("Failed to create bodyshop or vendor");
    }

    const updateBodyshop = await client.request(
      `mutation UPDATE_BODYSHOP($id: uuid!, $inhousevendorid: uuid!) {
        update_bodyshops_by_pk(pk_columns: { id: $id }, _set: { inhousevendorid: $inhousevendorid }) {
          id
        }
      }`,
      {
        id: bodyshopId,
        inhousevendorid: vendorId
      }
    );
    res.status(200).json(updateBodyshop);
  } catch (error) {
    logger.log("admin-create-shop-error", "error", req.user.email, null, {
      message: error.message,
      stack: error.stack,
      request: req.body,
      ioadmin: true
    });
    res.status(500).json(error);
  }
};
exports.updateCounter = async (req, res) => {
  logger.log("admin-update-counter", "debug", req.user.email, null, {
    request: req.body,
    ioadmin: true
  });
  const { id, counter } = req.body;

  try {
    const result = await client.request(
      `mutation UPDATE_COUNTER($id: uuid!, $counter: counters_set_input!) {
      update_counters_by_pk(pk_columns: { id: $id }, _set: $counter) {
        id
        countertype
        count
        prefix
      }
    }`,
      {
        id,
        counter
      }
    );
    res.json(result);
  } catch (error) {
    res.status(500).json(error);
  }
};
exports.updateShop = async (req, res) => {
  logger.log("admin-update-shop", "debug", req.user.email, null, {
    request: req.body,
    ioadmin: true
  });
  const { id, bodyshop } = req.body;

  try {
    const result = await client.request(
      `mutation UPDATE_BODYSHOP($id: uuid!, $bodyshop: bodyshops_set_input!) {
      update_bodyshops_by_pk(pk_columns: { id: $id }, _set: $bodyshop) {
        id

      }
    }`,
      {
        id,
        bodyshop
      }
    );
    res.json(result);
  } catch (error) {
    res.status(500).json(error);
  }
};
