import { applicationErrorResponse, sendFailedResponse, sendSuccessResponse } from "@lib/responses";
import { authenticateAPIToken } from "@middleware/authentication";
import { NextApiResponse } from "next";

export default async function handler(req: any, res: NextApiResponse) {
	authenticateAPIToken(req, res, async () => {
		try {
			if (req.websiteId) {
				const rawResponse = await fetch(
					`${process.env.CBEND_URL}/api/v1/bigcommerce/getReviewConfig?context=${req.query.context}`,
					{
						method: "POST",
						headers: {
							Accept: "application/json",
							"Content-Type": "application/json",
						},
						body: JSON.stringify({ websiteId: req.websiteId }),
					}
				);
				const content = await rawResponse.json();
				if (content.status === "success") {
					return sendSuccessResponse(
						res,
						content.data,
						"Review configuration fetched successfully"
					);
				}
				return sendFailedResponse(res, {}, content.message || "Error fetching review configuration");
			}
			return sendFailedResponse(res, {}, "Shop not found");
		} catch (e) {
			console.log(e);
			return applicationErrorResponse(res);
		}
	});
}
