const OAuthClient = require("intuit-oauth");
const logger = require("../../utils/logger");

const oauthClient = new OAuthClient({
  clientId: process.env.QBO_CLIENT_ID,
  clientSecret: process.env.QBO_SECRET,
  environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
  redirectUri: process.env.QBO_REDIRECT_URI
});

exports.default = async (req, res) => {
  try {
    logger.log("qbo-auth-uri", "DEBUG", req.user.email, null, null);
    const authUri = oauthClient.authorizeUri({
      scope: [OAuthClient.scopes.Accounting, OAuthClient.scopes.OpenId],
      state: req.user.email
    }); // can be an array of multiple scopes ex : {scope:[OAuthClient.scopes.Accounting,OAuthClient.scopes.OpenId]}

    res.send(authUri);
  } catch (error) {
    logger.log("qbo-auth-uri-error", "ERROR", req.user.email, null, { error });

    res.status(500).json(error);
  }
};
