const express = require("express");
const router = express.Router();
const logger = require("../../server/utils/logger");

// Pull secrets from env
const { VSSTA_INTEGRATION_SECRET, PARTS_MANAGEMENT_INTEGRATION_SECRET } = process.env;

// Only load VSSTA routes if the secret is set
if (typeof VSSTA_INTEGRATION_SECRET === "string" && VSSTA_INTEGRATION_SECRET.length > 0) {
  const vsstaIntegration = require("../integrations/VSSTA/vsstaIntegrationRoute");
  const vsstaMiddleware = require("../middleware/vsstaIntegrationMiddleware");

  router.post("/vssta", vsstaMiddleware, vsstaIntegration);
} else {
  logger.logger.warn("VSSTA_INTEGRATION_SECRET is not set — skipping /vssta integration route");
}

// Only load Parts Management routes if that secret is set
if (typeof PARTS_MANAGEMENT_INTEGRATION_SECRET === "string" && PARTS_MANAGEMENT_INTEGRATION_SECRET.length > 0) {
  const XML_BODY_LIMIT = "10mb"; // Set a limit for XML body size

  const {
    partsManagementProvisioning,
    patchPartsManagementProvisioning
  } = require("../integrations/partsManagement/endpoints/partsManagementProvisioning");
  const partsManagementDeprovisioning = require("../integrations/partsManagement/endpoints/partsManagementDeprovisioning");
  const partsManagementIntegrationMiddleware = require("../middleware/partsManagementIntegrationMiddleware");
  const partsManagementVehicleDamageEstimateAddRq = require("../integrations/partsManagement/endpoints/vehicleDamageEstimateAddRq");
  const partsManagementVehicleDamageEstimateChqRq = require("../integrations/partsManagement/endpoints/vehicleDamageEstimateChgRq");
  const patchJobStatus = require("../job/patchJobStatus");

  /**
   * Route to handle Vehicle Damage Estimate Add Request
   */
  router.post(
    "/parts-management/VehicleDamageEstimateAddRq",
    express.raw({ type: "application/xml", limit: XML_BODY_LIMIT }), // Parse XML body
    partsManagementIntegrationMiddleware,
    partsManagementVehicleDamageEstimateAddRq
  );

  /**
   * Route to handle Vehicle Damage Estimate Change Request
   */
  router.post(
    "/parts-management/VehicleDamageEstimateChgRq",
    express.raw({ type: "application/xml", limit: XML_BODY_LIMIT }), // Parse XML body
    partsManagementIntegrationMiddleware,
    partsManagementVehicleDamageEstimateChqRq
  );

  // Deprovisioning route
  if (process.env.NODE_ENV !== "production" && !process.env.HOSTNAME?.endsWith("compute.internal")) {
    logger.logger.warn("Parts Management Deprovisioning route has been loaded.");
    router.post("/parts-management/deprovision", partsManagementIntegrationMiddleware, partsManagementDeprovisioning);
  }

  /**
   * Route to handle Parts Management Provisioning
   */
  router.post("/parts-management/provision", partsManagementIntegrationMiddleware, partsManagementProvisioning);

  /**
   * PATCH route to update Parts Management Provisioning info
   */
  router.patch(
    "/parts-management/provision/:id",
    partsManagementIntegrationMiddleware,
    patchPartsManagementProvisioning
  );

  /**
   * PATCH route to update job status (parts management only)
   */
  router.patch("/parts-management/job/:id/status", partsManagementIntegrationMiddleware, patchJobStatus);
} else {
  logger.logger.warn("PARTS_MANAGEMENT_INTEGRATION_SECRET is not set — skipping /parts-management/provision route");
}

module.exports = router;
