import { RouteId } from "@archestra/shared";
import fastifyHttpProxy from "@fastify/http-proxy";
import type { FastifyPluginAsyncZod } from "fastify-type-provider-zod";
import { z } from "zod";
import config from "@/config";
import logger from "@/logging";
import {
  constructResponseSchema,
  OpenAi,
  UuidIdSchema,
  Zhipuai,
} from "@/types";
import {
  makeOpenAiCompatibleEmbeddingsAdapterFactory,
  zhipuaiAdapterFactory,
} from "../adapters";
import { PROXY_API_PREFIX, PROXY_BODY_LIMIT } from "../common";
import { handleLLMProxy } from "../llm-proxy-handler";
import { createProxyPreHandler } from "./proxy-prehandler";

const zhipuaiEmbeddingsAdapterFactory =
  makeOpenAiCompatibleEmbeddingsAdapterFactory(
    "zhipuai",
    () => config.llm.zhipuai.baseUrl,
  );

const zhipuaiProxyRoutes: FastifyPluginAsyncZod = async (fastify) => {
  const API_PREFIX = `${PROXY_API_PREFIX}/zhipuai`;
  const CHAT_COMPLETIONS_SUFFIX = "/chat/completions";
  const EMBEDDINGS_SUFFIX = "/embeddings";

  logger.info("[UnifiedProxy] Registering unified Zhipu AI routes");

  await fastify.register(fastifyHttpProxy, {
    upstream: config.llm.zhipuai.baseUrl,
    prefix: API_PREFIX,
    rewritePrefix: "",
    preHandler: createProxyPreHandler({
      apiPrefix: API_PREFIX,
      endpointSuffix: [CHAT_COMPLETIONS_SUFFIX, EMBEDDINGS_SUFFIX],
      upstream: config.llm.zhipuai.baseUrl,
      providerName: "Zhipu AI",
    }),
  });

  fastify.post(
    `${API_PREFIX}${EMBEDDINGS_SUFFIX}`,
    {
      bodyLimit: PROXY_BODY_LIMIT,
      schema: {
        operationId: RouteId.ZhipuaiEmbeddingsWithDefaultAgent,
        description: "Create embeddings with Zhipu AI (uses default agent)",
        tags: ["LLM Proxy"],
        body: OpenAi.API.EmbeddingRequestSchema,
        headers: OpenAi.API.ChatCompletionsHeadersSchema,
        response: constructResponseSchema(OpenAi.API.EmbeddingResponseSchema),
      },
    },
    async (request, reply) => {
      logger.debug(
        { url: request.url },
        "[UnifiedProxy] Handling Zhipu AI embeddings request (default agent)",
      );
      return handleLLMProxy(
        request.body as OpenAi.Types.EmbeddingRequest,
        request,
        reply,
        zhipuaiEmbeddingsAdapterFactory,
      );
    },
  );

  fastify.post(
    `${API_PREFIX}/:agentId${EMBEDDINGS_SUFFIX}`,
    {
      bodyLimit: PROXY_BODY_LIMIT,
      schema: {
        operationId: RouteId.ZhipuaiEmbeddingsWithAgent,
        description: "Create embeddings with Zhipu AI for a specific agent",
        tags: ["LLM Proxy"],
        params: z.object({
          agentId: UuidIdSchema,
        }),
        body: OpenAi.API.EmbeddingRequestSchema,
        headers: OpenAi.API.ChatCompletionsHeadersSchema,
        response: constructResponseSchema(OpenAi.API.EmbeddingResponseSchema),
      },
    },
    async (request, reply) => {
      logger.debug(
        { url: request.url, agentId: request.params.agentId },
        "[UnifiedProxy] Handling Zhipu AI embeddings request (with agent)",
      );
      return handleLLMProxy(
        request.body as OpenAi.Types.EmbeddingRequest,
        request,
        reply,
        zhipuaiEmbeddingsAdapterFactory,
      );
    },
  );

  fastify.post(
    `${API_PREFIX}${CHAT_COMPLETIONS_SUFFIX}`,
    {
      bodyLimit: PROXY_BODY_LIMIT,
      schema: {
        operationId: RouteId.ZhipuaiChatCompletionsWithDefaultAgent,
        description:
          "Create a chat completion with Zhipu AI (uses default agent)",
        tags: ["LLM Proxy"],
        body: Zhipuai.API.ChatCompletionRequestSchema,
        headers: Zhipuai.API.ChatCompletionsHeadersSchema,
        response: constructResponseSchema(
          Zhipuai.API.ChatCompletionResponseSchema,
        ),
      },
    },
    async (request, reply) => {
      logger.debug(
        { url: request.url },
        "[UnifiedProxy] Handling Zhipu AI request (default agent)",
      );
      return handleLLMProxy(
        request.body,
        request,
        reply,
        zhipuaiAdapterFactory,
      );
    },
  );

  fastify.post(
    `${API_PREFIX}/:agentId${CHAT_COMPLETIONS_SUFFIX}`,
    {
      bodyLimit: PROXY_BODY_LIMIT,
      schema: {
        operationId: RouteId.ZhipuaiChatCompletionsWithAgent,
        description:
          "Create a chat completion with Zhipu AI for a specific agent",
        tags: ["LLM Proxy"],
        params: z.object({
          agentId: UuidIdSchema,
        }),
        body: Zhipuai.API.ChatCompletionRequestSchema,
        headers: Zhipuai.API.ChatCompletionsHeadersSchema,
        response: constructResponseSchema(
          Zhipuai.API.ChatCompletionResponseSchema,
        ),
      },
    },
    async (request, reply) => {
      logger.debug(
        { url: request.url, agentId: request.params.agentId },
        "[UnifiedProxy] Handling Zhipu AI request (with agent)",
      );
      return handleLLMProxy(
        request.body,
        request,
        reply,
        zhipuaiAdapterFactory,
      );
    },
  );
};

export default zhipuaiProxyRoutes;
