import { Hono } from "hono";
import { withConsoleUserSession } from "@/middlewares/withConsoleUserSession";
import { withConsoleUserRole } from "@/middlewares/console-role";
import {
  approveKnowledgeHandler,
  createKnowledgeHandler,
  flagStatsHandler,
  getConversationHandler,
  getFlagHandler,
  listFlagsHandler,
  listKnowledgeHandler,
  manualFlagHandler,
  publishHandler,
  publishLogHandler,
  publishPreviewHandler,
  retireKnowledgeHandler,
  routeFlagToClickUpHandler,
  updateFlagHandler,
  updateKnowledgeHandler,
} from "@/controllers/admin/kevo/kevo.controller";

/**
 * Console-facing Kevo endpoints (/v1/admin-console/kevo). RBAC per PLAN.md:
 *   kevo.read    view flags / knowledge / stats / transcripts
 *   kevo.create  manual flags + write corrections
 *   kevo.update  review actions (status, notes, ClickUp routing)
 *   kevo.delete  retire knowledge entries
 *   kevo-publish.update  publish to the ElevenLabs agent
 */
export const KevoAdminRoutes = new Hono();

KevoAdminRoutes.use("*", withConsoleUserSession());

// Flags
KevoAdminRoutes.post(
  "/flags/list",
  withConsoleUserRole("kevo", "read"),
  listFlagsHandler,
);
KevoAdminRoutes.get(
  "/flags/stats",
  withConsoleUserRole("kevo", "read"),
  flagStatsHandler,
);
KevoAdminRoutes.post(
  "/flags/manual",
  withConsoleUserRole("kevo", "create"),
  manualFlagHandler,
);
KevoAdminRoutes.get(
  "/flags/:reference",
  withConsoleUserRole("kevo", "read"),
  getFlagHandler,
);
KevoAdminRoutes.patch(
  "/flags/:reference",
  withConsoleUserRole("kevo", "update"),
  updateFlagHandler,
);
KevoAdminRoutes.post(
  "/flags/:reference/clickup",
  withConsoleUserRole("kevo", "update"),
  routeFlagToClickUpHandler,
);

// Conversations (on-demand transcript proxy)
KevoAdminRoutes.get(
  "/conversations/:conversationId",
  withConsoleUserRole("kevo", "read"),
  getConversationHandler,
);

// Knowledge entries
KevoAdminRoutes.post(
  "/knowledge/list",
  withConsoleUserRole("kevo", "read"),
  listKnowledgeHandler,
);
KevoAdminRoutes.post(
  "/knowledge",
  withConsoleUserRole("kevo", "create"),
  createKnowledgeHandler,
);
KevoAdminRoutes.patch(
  "/knowledge/:id",
  withConsoleUserRole("kevo", "create"),
  updateKnowledgeHandler,
);
KevoAdminRoutes.post(
  "/knowledge/:id/approve",
  withConsoleUserRole("kevo", "update"),
  approveKnowledgeHandler,
);
KevoAdminRoutes.post(
  "/knowledge/:id/retire",
  withConsoleUserRole("kevo", "delete"),
  retireKnowledgeHandler,
);

// Publishing
KevoAdminRoutes.get(
  "/publish/preview",
  withConsoleUserRole("kevo", "read"),
  publishPreviewHandler,
);
KevoAdminRoutes.get(
  "/publish/log",
  withConsoleUserRole("kevo", "read"),
  publishLogHandler,
);
KevoAdminRoutes.post(
  "/publish",
  withConsoleUserRole("kevo-publish", "update"),
  publishHandler,
);
