export default ({ env }) => {
  const isProduction = env("NODE_ENV") === "production";

  return [
    "strapi::logger",
    {
      name: "strapi::errors",
      config: {
        // Hide detailed error stack traces in production
        ...(isProduction && {
          format: (err: any) => {
            // Return generic error message in production
            return {
              message: err.message || "An error occurred",
              // Don't expose stack trace, file paths, or internal details
              ...(process.env.STRAPI_DISABLE_ERROR_STACK !== "true" &&
                !isProduction && {
                  error: err,
                }),
            };
          },
        }),
      },
    },
    {
      name: "strapi::security",
      config: {
        contentSecurityPolicy: {
          useDefaults: true,
          directives: {
            "connect-src": ["'self'", "https:"],
            "img-src": [
              "'self'",
              "data:",
              "blob:",
              "https://cms.navana-realestate.com",
              "https://nrel.navana-realestate.com",
            ],
            "media-src": [
              "'self'",
              "data:",
              "blob:",
              "https://cms.navana-realestate.com",
              "https://nrel.navana-realestate.com",
            ],
            upgradeInsecureRequests: null,
          },
        },
      },
    },
    {
      name: "strapi::cors",
      config: {
        enabled: true,
        headers: [
          "Content-Type",
          "Authorization",
          "Origin",
          "Accept",
          "X-Requested-With",
        ],
        origin: [
          "https://nrel.navana-realestate.com",
          "https://cms.navana-realestate.com",
          "http://localhost:3000",
          "http://localhost:1337",
        ],
      },
    },
    "global::block-register-route",
    "strapi::poweredBy",
    "strapi::query",
    {
      name: "strapi::body",
      config: {
        jsonLimit: "10mb",
        formLimit: "100mb",
        textLimit: "10mb",
        formidable: {
          // 500MB max to reduce DoS risk; increase only if large video uploads are required
          maxFileSize: 1024 * 1024 * 500,
        },
      },
    },
    "global::sanitize-footer-components",
    "strapi::session",
    "strapi::favicon",
    "strapi::public",
  ];
};
