/*
 * Priority Forge — REST shims for the SharedTask orchestration MCP tools
 *
 * Mirrors the xmem.ts pattern: each REST handler delegates to
 * handleToolCall(name, params) so the REST and MCP surfaces share their
 * dispatch path and stay structurally identical. Bridge logic
 * (projection materialization, archival) lives in
 * src/orchestration/shared-tasks.ts; nothing here.
 */

import { Router, Request, Response } from 'express';
import { handleToolCall } from '../mcp/handler';

const router = Router();

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

// ── Reads ─────────────────────────────────────────────────────────────

// GET /shared-tasks/list?status=...&limit=...
router.get('/list', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.status === 'string') params.status = req.query.status;
  if (typeof req.query.limit === 'string') params.limit = Number(req.query.limit);
  await call('shared_task_list', params, res);
});

// GET /shared-tasks/get?sharedTaskId=...
router.get('/get', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.sharedTaskId === 'string') params.sharedTaskId = req.query.sharedTaskId;
  await call('shared_task_get', params, res);
});

// GET /shared-tasks/history?sharedTaskId=...
router.get('/history', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.sharedTaskId === 'string') params.sharedTaskId = req.query.sharedTaskId;
  await call('shared_task_history', params, res);
});

// GET /shared-tasks/timeline?sharedTaskId=...
router.get('/timeline', async (req: Request, res: Response) => {
  const params: Record<string, unknown> = {};
  if (typeof req.query.sharedTaskId === 'string') params.sharedTaskId = req.query.sharedTaskId;
  await call('shared_task_timeline', params, res);
});

// GET /shared-tasks/my-claims
router.get('/my-claims', async (_req: Request, res: Response) => {
  await call('shared_task_get_my_claims', {}, res);
});

// ── Lifecycle ─────────────────────────────────────────────────────────

// POST /shared-tasks/create { title, description? }
router.post('/create', async (req: Request, res: Response) => {
  await call('shared_task_create', (req.body ?? {}) as Record<string, unknown>, res);
});

// POST /shared-tasks/publish { localTaskId, ttlMs? }
router.post('/publish', async (req: Request, res: Response) => {
  await call('shared_task_publish', (req.body ?? {}) as Record<string, unknown>, res);
});

// POST /shared-tasks/claim { sharedTaskId, ttlMs?, priority?, effort?, project? }
router.post('/claim', async (req: Request, res: Response) => {
  await call('shared_task_claim', (req.body ?? {}) as Record<string, unknown>, res);
});

// POST /shared-tasks/release { sharedTaskId, outcome }
router.post('/release', async (req: Request, res: Response) => {
  await call('shared_task_release', (req.body ?? {}) as Record<string, unknown>, res);
});

// POST /shared-tasks/renew { sharedTaskId, ttlMs? }
router.post('/renew', async (req: Request, res: Response) => {
  await call('shared_task_renew_lease', (req.body ?? {}) as Record<string, unknown>, res);
});

export default router;
