/*
 * Priority Forge — REST shims for the memory-hub MCP tools
 *
 * Frontend hits REST, not MCP — this matches the convention used by every
 * other PF route file. Each handler delegates to dispatchXmemTool so the
 * MCP and REST surfaces stay structurally identical (one source of truth
 * for argument shape and return shape).
 */

import { Router, Request, Response } from 'express';
import { dispatchXmemTool } from 'priority-forge-hub';

const router = Router();

async function call(name: string, params: Record<string, unknown>, res: Response): Promise<void> {
  try {
    const result = await dispatchXmemTool(name, params);
    res.json(result);
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    const status = /required|invalid/i.test(msg) ? 400 : 500;
    res.status(status).json({ error: msg });
  }
}

// GET /xmem/status
router.get('/status', async (_req: Request, res: Response) => {
  await call('xmem_get_status', {}, res);
});

// GET /xmem/team-activity?limit=50&sinceTs=...&agentIds=a,b
router.get('/team-activity', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.teamId === 'string') params.teamId = req.query.teamId;
  if (typeof req.query.sinceTs === 'string') params.sinceTs = req.query.sinceTs;
  if (typeof req.query.limit === 'string') params.limit = Number(req.query.limit);
  if (typeof req.query.agentIds === 'string') {
    params.agentIds = req.query.agentIds.split(',').map((s) => s.trim()).filter(Boolean);
  }
  await call('xmem_get_team_activity', params, res);
});

// GET /xmem/blocking-me?agentId=...
router.get('/blocking-me', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.agentId === 'string') params.agentId = req.query.agentId;
  await call('xmem_get_blocking_me', params, res);
});

// GET /xmem/i-am-blocking?agentId=...
router.get('/i-am-blocking', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.agentId === 'string') params.agentId = req.query.agentId;
  await call('xmem_get_i_am_blocking', params, res);
});

// GET /xmem/current-focus?agentIds=a,b — who is working on what right now
router.get('/current-focus', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.teamId === 'string') params.teamId = req.query.teamId;
  if (typeof req.query.agentIds === 'string') {
    params.agentIds = req.query.agentIds.split(',').map((s) => s.trim()).filter(Boolean);
  }
  await call('xmem_get_current_focus', params, res);
});

// POST /xmem/search { query, teamId?, limit? }
router.post('/search', async (req: Request, res: Response) => {
  const body = (req.body ?? {}) as Record<string, unknown>;
  await call('xmem_search_team_memory', body, res);
});

// POST /xmem/rederive — manual blocker rederivation (debug / ops)
router.post('/rederive', async (_req: Request, res: Response) => {
  await call('xmem_force_rederive_blockers', {}, res);
});

export default router;
