const jwt = require('jsonwebtoken');
const SECRET = process.env.JWT_SECRET || 'pulse-mv-secret-key-2024';

module.exports = (req, res, next) => {
  const authHeader = req.headers.authorization;
  if (!authHeader) {
    return res.status(401).json({ error: 'އަދި ލޮގިންއެއް ނުވޭ' }); // Not logged in
  }

  const token = authHeader.split(' ')[1];
  try {
    const decoded = jwt.verify(token, SECRET);
    req.user = decoded;
    next();
  } catch (err) {
    res.status(403).json({ error: 'ޓޯކަން ސައްޙައެއް ނޫން' }); // Invalid token
  }
};
