import { Router, Request, Response } from 'express';
import { param } from '../utils/params';
import { registerSlug, unregisterSlug, listSlugs } from '../services/registry.service';

const router = Router();

// POST /instances/register — Register a slug→basePath mapping
router.post('/instances/register', async (req: Request, res: Response) => {
  const { slug, basePath, composeProject } = req.body;
  if (!slug || !basePath || !composeProject) {
    res.status(400).json({ error: 'VALIDATION', message: 'slug, basePath, and composeProject required' });
    return;
  }
  await registerSlug(slug, basePath, composeProject);
  res.json({ registered: slug });
});

// DELETE /instances/:slug — Unregister slug
router.delete('/instances/:slug', async (req: Request, res: Response) => {
  await unregisterSlug(param(req, 'slug'));
  res.json({ unregistered: param(req, 'slug') });
});

// GET /instances — List all managed slugs
router.get('/instances', async (_req: Request, res: Response) => {
  const slugs = await listSlugs();
  res.json(slugs);
});

export default router;
