import { V1Controllers } from "@/cmd/routes/v1/controllers";
import { UnlockPublicLinkSchema } from "@/cmd/routes/v1/controllers/dashboard.schema";
import { sanitizePayload } from "@/internal/middlewares/sanitzePayload";
import { Hono } from "hono";

/**
 * Public share-link viewer endpoints.
 *
 * Deliberately session-free and with NO shared-secret API key: the opaque
 * token in the URL IS the credential. Everything else about the perimeter is
 * unchanged from the authed path — same serving core, same headers, same
 * path-safety check — because a public viewer is strictly LESS trusted than a
 * console user, never more.
 *
 * Mounted at /v1/public/dashboards, following the existing /v1/public/* pattern.
 */
export const DashboardPublicRoutes = new Hono();

// Declared before the bare /:token/serve so the wildcard wins for sub-assets.
DashboardPublicRoutes.get("/:token/serve/*", async (ctx) =>
  V1Controllers(ctx).DashboardPublicController.Serve(),
);
DashboardPublicRoutes.get("/:token/serve", async (ctx) =>
  V1Controllers(ctx).DashboardPublicController.Serve(),
);

DashboardPublicRoutes.post(
  "/:token/unlock",
  sanitizePayload(UnlockPublicLinkSchema),
  async (ctx) => V1Controllers(ctx).DashboardPublicController.Unlock(),
);
