export function requireIngestKey(req, res, next) {
  const authHeader = req.headers.authorization;

  if (!authHeader || !authHeader.startsWith('Bearer ')) {
    return res.status(401).json({ error: 'API key required. Use Authorization: Bearer <key>' });
  }

  const apiKey = authHeader.slice(7);
  const validKey = process.env.INGEST_API_KEY;

  if (!validKey) {
    console.error('INGEST_API_KEY environment variable not set');
    return res.status(500).json({ error: 'API key not configured on server' });
  }

  if (apiKey !== validKey) {
    return res.status(403).json({ error: 'Invalid API key' });
  }

  next();
}
