/**
 * Session recording route handlers.
 *
 * Registers Hono routes for controlling per-session message recording.
 * Depends on an optional {@link RecorderManager} instance; returns 501 when
 * recording is not available.
 *
 * Routes:
 * - `POST /sessions/:id/recording/start` — Enable recording for a session.
 * - `POST /sessions/:id/recording/stop` — Disable recording for a session.
 * - `GET /sessions/:id/recording/status` — Get recording status for a session.
 * - `GET /recordings` — List all saved recordings.
 */

import type { Hono } from "hono";
import { z } from "zod";
import type { RecorderManager } from "../recorder.js";
import { parseParam } from "../validation.js";

// ─── Zod Schemas ──────────────────────────────────────────────────

const sessionIdSchema = z.string().min(1, "Session ID is required");

// ─── Routes ───────────────────────────────────────────────────────

export function registerRecordingRoutes(
  api: Hono,
  recorder: RecorderManager | undefined,
): void {
  api.post("/sessions/:id/recording/start", (c) => {
    const parsed = parseParam(c, "id", sessionIdSchema);
    if (!parsed.success) return parsed.response;
    const id = parsed.data;
    if (!recorder) return c.json({ error: "Recording not available" }, 501);
    recorder.enableForSession(id);
    return c.json({ ok: true, recording: true });
  });

  api.post("/sessions/:id/recording/stop", (c) => {
    const parsed = parseParam(c, "id", sessionIdSchema);
    if (!parsed.success) return parsed.response;
    const id = parsed.data;
    if (!recorder) return c.json({ error: "Recording not available" }, 501);
    recorder.disableForSession(id);
    return c.json({ ok: true, recording: false });
  });

  api.get("/sessions/:id/recording/status", (c) => {
    const parsed = parseParam(c, "id", sessionIdSchema);
    if (!parsed.success) return parsed.response;
    const id = parsed.data;
    if (!recorder) return c.json({ recording: false, available: false });
    return c.json({
      recording: recorder.isRecording(id),
      available: true,
      ...recorder.getRecordingStatus(id),
    });
  });

  api.get("/recordings", async (c) => {
    if (!recorder) return c.json({ recordings: [] });
    return c.json({ recordings: await recorder.listRecordings() });
  });
}
