import { Hono } from "hono";
import * as z from "zod";

import { getCopilotHistory, streamCopilot } from "@turbostarter/ai/copilot/api";
import { copilotMessageSchema } from "@turbostarter/ai/copilot/schema";

import { enforceAuth, deductCredits, rateLimiter, validate } from "../../../middleware";

import type { User } from "@turbostarter/auth";

const chatIdQuerySchema = z.object({
  chatId: z.string(),
});

export const copilotRouter = new Hono<{
  Variables: {
    user: User;
  };
}>()
  .get(
    "/messages",
    enforceAuth,
    validate("query", chatIdQuerySchema),
    async (c) => {
      const { chatId } = c.req.valid("query");
      const messages = await getCopilotHistory(chatId);
      return c.json(messages);
    },
  )
  .post(
    "/",
    enforceAuth,
    rateLimiter,
    validate("json", copilotMessageSchema),
    async (c) => {
      const input = c.req.valid("json");

      // Deduct 1 credit per copilot message
      await deductCredits(1, "copilot")(c, async () => { /* noop */ });

      return streamCopilot({
        ...input,
        signal: c.req.raw.signal,
        userId: c.var.user.id,
      });
    },
  );
