import { Hono } from "hono";
import { withConsoleUserSession } from "@/middlewares/withConsoleUserSession";
import { withConsoleUserRole } from "@/middlewares/console-role";
import { withSuperAdmin } from "@/middlewares/withSuperAdmin";
import {
  addAllowlistHandler,
  addAttachmentHandler,
  analyticsHandler,
  bindConversationHandler,
  closeTicketHandler,
  confirmActionHandler,
  declineActionHandler,
  listTicketActionsHandler,
  createTicketHandler,
  getAnyTicketHandler,
  getAnyTicketTranscriptHandler,
  getAnyTicketUpdatesHandler,
  getMyTicketHandler,
  getMyTicketTranscriptHandler,
  getMyTicketUpdatesHandler,
  listAllowlistHandler,
  listAllTicketsHandler,
  listAuditHandler,
  listMyTicketsHandler,
  removeAllowlistHandler,
  startSessionHandler,
} from "@/controllers/admin/guru/guru.controller";

/**
 * Console-facing Subito AI Guru endpoints (/v1/admin-console/guru).
 * RBAC per docs/subito-ai-guru.md:
 *   guru.create       open tickets / start chats
 *   guru.read         own tickets only (ownership enforced in the controller)
 *   guru-admin.read   all tickets, analytics
 *   super admin       edit allowlist + audit log (spec §3)
 *
 * Per-concierge visibility is enforced by separate routes with separate
 * guards — the "my" handlers scope by the session user id, never by a
 * client-supplied id.
 */
export const GuruAdminRoutes = new Hono();

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

// Tickets — concierge (own only)
GuruAdminRoutes.post(
  "/tickets",
  withConsoleUserRole("guru", "create"),
  createTicketHandler,
);
GuruAdminRoutes.post(
  "/tickets/list",
  withConsoleUserRole("guru", "read"),
  listMyTicketsHandler,
);
GuruAdminRoutes.get(
  "/tickets/:reference",
  withConsoleUserRole("guru", "read"),
  getMyTicketHandler,
);
GuruAdminRoutes.get(
  "/tickets/:reference/actions",
  withConsoleUserRole("guru", "read"),
  listTicketActionsHandler,
);
// Confirm/decline run server-side allowlist + ownership checks on top of
// the role guard — the role alone never authorizes a member-data write.
GuruAdminRoutes.post(
  "/actions/:id/confirm",
  withConsoleUserRole("guru", "update"),
  confirmActionHandler,
);
GuruAdminRoutes.post(
  "/actions/:id/decline",
  withConsoleUserRole("guru", "update"),
  declineActionHandler,
);
GuruAdminRoutes.post(
  "/tickets/:reference/attachments",
  withConsoleUserRole("guru", "create"),
  addAttachmentHandler,
);
GuruAdminRoutes.post(
  "/tickets/:reference/close",
  withConsoleUserRole("guru", "update"),
  closeTicketHandler,
);
GuruAdminRoutes.get(
  "/tickets/:reference/transcript",
  withConsoleUserRole("guru", "read"),
  getMyTicketTranscriptHandler,
);
GuruAdminRoutes.get(
  "/admin/tickets/:reference/transcript",
  withConsoleUserRole("guru-admin", "read"),
  getAnyTicketTranscriptHandler,
);
GuruAdminRoutes.get(
  "/tickets/:reference/updates",
  withConsoleUserRole("guru", "read"),
  getMyTicketUpdatesHandler,
);
GuruAdminRoutes.get(
  "/admin/tickets/:reference/updates",
  withConsoleUserRole("guru-admin", "read"),
  getAnyTicketUpdatesHandler,
);
GuruAdminRoutes.post(
  "/tickets/:reference/session",
  withConsoleUserRole("guru", "create"),
  startSessionHandler,
);
GuruAdminRoutes.post(
  "/tickets/:reference/conversations",
  withConsoleUserRole("guru", "create"),
  bindConversationHandler,
);

// Tickets — admin (all)
GuruAdminRoutes.post(
  "/admin/tickets/list",
  withConsoleUserRole("guru-admin", "read"),
  listAllTicketsHandler,
);
GuruAdminRoutes.get(
  "/admin/tickets/:reference",
  withConsoleUserRole("guru-admin", "read"),
  getAnyTicketHandler,
);

// Analytics — admin view
GuruAdminRoutes.get(
  "/analytics",
  withConsoleUserRole("guru-admin", "read"),
  analyticsHandler,
);

// Edit allowlist — super admin only
GuruAdminRoutes.get("/allowlist", withSuperAdmin(), listAllowlistHandler);
GuruAdminRoutes.post("/allowlist", withSuperAdmin(), addAllowlistHandler);
GuruAdminRoutes.delete("/allowlist", withSuperAdmin(), removeAllowlistHandler);

// Audit log — super admin only
GuruAdminRoutes.post("/audit/list", withSuperAdmin(), listAuditHandler);
