import { Hono } from "hono";
import { withConsoleUserSession } from "@/middlewares/withConsoleUserSession";
import {
  sanitizePayload,
  sanitizeQueryParams,
} from "@/internal/middlewares/sanitzePayload";
import { V1Controllers } from "@/cmd/routes/v1/controllers";
import {
  CreateAgentSchema,
  UpdateAgentSchema,
  CreatePromptSchema,
  UpdatePromptSchema,
  ListQuerySchema,
} from "@/cmd/routes/v1/controllers/chatbot.schema";

export const PromptsAndAgentsRoutes = new Hono();

// History
PromptsAndAgentsRoutes.get(
  "/history",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.GetChatbotHistory(),
);

// Test Agent (no session required, admin auth only)
PromptsAndAgentsRoutes.post(
  "/agents/test",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.TestAgent(),
);

// Global Agent
PromptsAndAgentsRoutes.get(
  "/global-agent",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.GetGlobalAgent(),
);

PromptsAndAgentsRoutes.put(
  "/global-agent",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.UpdateGlobalAgent(),
);

// Agents
PromptsAndAgentsRoutes.post(
  "/agents",
  withConsoleUserSession(),
  sanitizePayload(CreateAgentSchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.CreateAgent(),
);

PromptsAndAgentsRoutes.get(
  "/agents",
  withConsoleUserSession(),
  sanitizeQueryParams(ListQuerySchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.ListAgents(),
);

PromptsAndAgentsRoutes.get(
  "/agents/:id",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.GetAgentById(),
);

PromptsAndAgentsRoutes.put(
  "/agents/:id",
  withConsoleUserSession(),
  sanitizePayload(UpdateAgentSchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.UpdateAgent(),
);

PromptsAndAgentsRoutes.delete(
  "/agents/:id",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.DeleteAgent(),
);

// File upload
PromptsAndAgentsRoutes.post(
  "/upload",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.UploadFiles(),
);

// Make agent global
PromptsAndAgentsRoutes.patch(
  "/agents/:id/make-global",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.MakeAgentGlobal(),
);

// Toggle agent active/inactive
PromptsAndAgentsRoutes.patch(
  "/agents/:id/toggle",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.ToggleAgent(),
);

// Prompts
PromptsAndAgentsRoutes.post(
  "/prompts",
  withConsoleUserSession(),
  sanitizePayload(CreatePromptSchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.CreatePrompt(),
);

PromptsAndAgentsRoutes.get(
  "/prompts",
  withConsoleUserSession(),
  sanitizeQueryParams(ListQuerySchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.ListPrompts(),
);

PromptsAndAgentsRoutes.get(
  "/prompts/:id",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.GetPromptById(),
);

PromptsAndAgentsRoutes.put(
  "/prompts/:id",
  withConsoleUserSession(),
  sanitizePayload(UpdatePromptSchema),
  async (ctx) => V1Controllers(ctx).ChatbotController.UpdatePrompt(),
);

PromptsAndAgentsRoutes.delete(
  "/prompts/:id",
  withConsoleUserSession(),
  async (ctx) => V1Controllers(ctx).ChatbotController.DeletePrompt(),
);
