/**
 * Basic Authentication Middleware for Swagger Docs
 */
export const docsAuth = (req, res, next) => {
    const authHeader = req.headers.authorization;
  
    if (!authHeader) {
      res.setHeader('WWW-Authenticate', 'Basic realm="Airpero API Docs"');
      return res.status(401).json({ success: false, message: "Authentication required for API Docs" });
    }
  
    const auth = Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(':');
    const user = auth[0];
    const pass = auth[1];
  
    const expectedUser = process.env.DOCS_USER || 'admin';
    const expectedPass = process.env.DOCS_PASS || 'airpero_docs_2026';
  
    if (user === expectedUser && pass === expectedPass) {
      next();
    } else {
      res.setHeader('WWW-Authenticate', 'Basic realm="Airpero API Docs"');
      return res.status(401).json({ success: false, message: "Invalid credentials for API Docs" });
    }
  };
