import { Router } from 'express';
import db from '../db.js';
import { authMiddleware, adminOnly } from '../middleware/auth.js';
import { getEmailConfig } from '../email/transporter.js';
import { pollInbox } from '../email/imap.js';

const router = Router();
router.use(authMiddleware);

router.get('/config', adminOnly, (req, res) => {
  const config = getEmailConfig();
  if (!config) return res.status(404).json({ error: 'Not configured' });
  res.json({ ...config, app_password: '••••••••' });
});

router.get('/log', adminOnly, (req, res) => {
  const logs = db.prepare('SELECT * FROM email_log ORDER BY created_at DESC LIMIT 100').all();
  res.json(logs);
});

router.post('/poll', adminOnly, async (req, res) => {
  try {
    await pollInbox();
    res.json({ success: true });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

router.get('/stats', adminOnly, (req, res) => {
  const sent = db.prepare("SELECT COUNT(*) as c FROM email_log WHERE direction = 'sent'").get().c;
  const received = db.prepare("SELECT COUNT(*) as c FROM email_log WHERE direction = 'received'").get().c;
  const failed = db.prepare("SELECT COUNT(*) as c FROM email_log WHERE status = 'failed'").get().c;
  res.json({ sent, received, failed });
});

export default router;
