import { Router } from 'express';
import jwt from 'jsonwebtoken';

const router = Router();

router.post('/login', async (req, res) => {
  const { email, password } = req.body || {};
  const ADMIN_EMAIL = process.env.ADMIN_EMAIL || 'admin@supersunday.local';
  const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'changeme';
  if (email === ADMIN_EMAIL && password === ADMIN_PASSWORD) {
    const token = jwt.sign({ sub: email, role: 'admin' }, process.env.JWT_SECRET || 'supersecret', { expiresIn: '12h' });
    return res.json({ token });
  }
  return res.status(401).json({ error: 'bad_credentials' });
});

export default router;
