import { Router } from 'express';
import { contentLoader } from '../services/ContentLoader.js';
import { progressionSystem } from '../services/ProgressionSystem.js';
import { vmManager } from '../services/VMManager.js';

const router = Router();

router.get('/', async (_req, res) => {
  const vms = await Promise.all(
    [...contentLoader.vmProfiles.values()]
      .sort((left, right) => left.id.localeCompare(right.id))
      .map(async (profile) => ({
        id: profile.id,
        domain: vmManager.getDomainName(profile.id),
        hostname: profile.hostname ?? profile.id,
        state: await vmManager.getState(profile.id),
        unlocked: profile.id === 'workstation' ? true : progressionSystem.hasVM(profile.id)
      }))
  );

  res.json(vms);
});

export default router;
