import { Elysia, t } from "elysia";
import { getChatHistory } from "../services/chat";

export const chatController = new Elysia({ prefix: "/sessions" })
  .get(
    "/:id/messages",
    async ({ params, query }) => {
      const { id } = params;
      const limit = query.limit ? parseInt(query.limit) : 100;

      try {
        const messages = await getChatHistory(id, limit);

        return {
          success: true,
          messages,
          count: messages.length,
        };
      } catch (error) {
        console.error("[Chat API] Error fetching chat history:", error);
        return {
          success: false,
          error: "Failed to fetch chat history",
          messages: [],
          count: 0,
        };
      }
    },
    {
      params: t.Object({
        id: t.String(),
      }),
      query: t.Object({
        limit: t.Optional(t.String()),
      }),
    }
  );

