import { Router } from 'express';
import { generateSurgeConfig } from '../services/generator.js';

const router = Router();

// GET /surge - Surge client subscription endpoint (no auth required)
router.get('/', (req, res) => {
  const host = req.headers.host || 'localhost:3456';
  const protocol = req.secure ? 'https' : 'http';
  const hostUrl = `${protocol}://${host}/surge`;

  const config = generateSurgeConfig(hostUrl);

  res.set({
    'Content-Type': 'text/plain; charset=utf-8',
    'Content-Disposition': 'attachment; filename=sub-router.conf',
  });
  res.send(config);
});

// GET /api/config/preview - preview generated config
router.get('/preview', (req, res) => {
  const host = req.headers.host || 'localhost:3456';
  const protocol = req.secure ? 'https' : 'http';
  const hostUrl = `${protocol}://${host}/surge`;

  const config = generateSurgeConfig(hostUrl);
  res.json({ config });
});

export default router;
