import { Router, type Request, type Response } from 'express';
import type { SessionRegistry } from '../ssh/console-registry.js';

export function createConsoleAdminRouter(deps: {
  registry: SessionRegistry;
  requireAdmin: any;
}): Router {
  const r = Router();
  r.get('/ssh/console-sessions', deps.requireAdmin, (_req: Request, res: Response) => {
    const sessions = deps.registry.listAll().map((s) => ({
      task_id: s.localTaskId,
      owner_id: s.ownerId,
      connection_id: s.connectionId,
      started_at: new Date(s.startedAt).toISOString(),
      last_activity_at: new Date(s.lastActivityAt).toISOString(),
      total_input_bytes: s.totalInputBytes,
      total_output_bytes: s.totalOutputBytes,
    }));
    res.json({ sessions });
  });
  r.post('/ssh/console-sessions/:taskId/kill', deps.requireAdmin, async (req: Request, res: Response) => {
    const taskId = req.params.taskId!;
    await deps.registry.closeForTask(taskId, 'admin_kill');
    res.json({ closed: true });
  });
  return r;
}
