/**
 * Runtime Context Middleware
 * Reads environment and project slug from headers for route-based access.
 * Routes: /p/[slug] (production), /p/[slug]/stage (stage), /constructor (dev)
 */

function runtimeContextMiddleware(req, res, next) {
  const context = {
    mode: 'admin',
    projectSlug: null,
  };

  // Read environment from header (X-Runtime-Environment)
  const headerEnvironment = req.headers['x-runtime-environment'];
  if (
    headerEnvironment &&
    ['production', 'stage', 'dev'].includes(headerEnvironment)
  ) {
    context.headerEnvironment = headerEnvironment;
  }

  // Read project slug from header (X-Runtime-Project-Slug)
  const headerProjectSlug = req.headers['x-runtime-project-slug'];
  if (headerProjectSlug) {
    context.headerProjectSlug = headerProjectSlug;
  }

  req.runtimeContext = context;
  next();
}

module.exports = {
  runtimeContextMiddleware,
};
