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

const router = Router();

router.post('/login', async (req, res) => {
  const { email, password } = req.body || {};
  if(!email || !password) return res.status(400).json({error:'email/password required'});
  const adminEmail = process.env.ADMIN_EMAIL || 'admin@supersunday.local';
  const adminPass = process.env.ADMIN_PASSWORD || 'admin1234';
  if(email === adminEmail && password === adminPass){
    const token = jwt.sign({ sub: email, role: 'admin' }, process.env.JWT_SECRET || 'devsecret', { expiresIn: '12h' });
    return res.json({ token });
  }
  return res.status(401).json({error:'Invalid credentials'});
});

export default router;
