"use client";
import { REQUEST_TIMEOUT_MS } from "@/app/constant";
import { useChatStore } from "@/app/store";
import {
  ChatOptions,
  LLMApi,
  LLMModel,
} from "../api";
import Locale from "../../locales";
import {
  EventStreamContentType,
  fetchEventSource,
} from "@fortaine/fetch-event-source";
import { prettyObject } from "@/app/utils/format";
import { getMessageTextContent } from "@/app/utils";
import { bigModelApiKey, knowledgeId, mask } from "../config";

export class BigModelApi implements LLMApi {
  path(): string {
    return 'https://open.bigmodel.cn/api/paas/v4/chat/completions'
  }

  async chat(options: ChatOptions) {
    const messages = options.messages.map((v) => ({
      role: v.role,
      content: getMessageTextContent(v),
    }));

    // if (messages.length % 2 === 0) {
    //   messages.unshift({
    //     role: "user",
    //     content: " ",
    //   });
    // }

    const shouldStream = true;

    if (mask.enabled) {
      messages.unshift({
        role: "user",
        content: mask.content,
      })
    }

    // 通用大模型参数
    const requestPayload: any = {
      messages: messages,
      stream: shouldStream,// 流式回复
      model: 'glm-4-0520',// 模型
      temperature: 0.01,// 采样温度
      top_p: 0.7,// 核取样
      tools: [
        {
          type: 'retrieval', // 工具类型为检索
          retrieval: {
            knowledge_id: knowledgeId,// 知识库ID
          },
        },
      ],
    };

    const controller = new AbortController();

    options.onController?.(controller);

    try {
      const chatPath = this.path();
      const chatPayload = {
        method: "POST",
        body: JSON.stringify(requestPayload),
        signal: controller.signal,
        headers: {
          'Content-Type': 'application/json',
          // APIKey
          Authorization: bigModelApiKey
        },
      };

      // make a fetch request
      const requestTimeoutId = setTimeout(
        () => controller.abort(),
        REQUEST_TIMEOUT_MS,
      );

      if (shouldStream) {
        let responseText = "";
        let remainText = "";
        let finished = false;

        // animate response to make it looks smooth
        function animateResponseText() {
          if (finished || controller.signal.aborted) {
            responseText += remainText;
            if (responseText?.length === 0) {
              options.onError?.(new Error("empty response from server"));
            }
            return;
          }

          if (remainText.length > 0) {
            const fetchCount = Math.max(1, Math.round(remainText.length / 60));
            const fetchText = remainText.slice(0, fetchCount);
            responseText += fetchText;
            remainText = remainText.slice(fetchCount);
            options.onUpdate?.(responseText, fetchText);
          }

          requestAnimationFrame(animateResponseText);
        }

        // start animaion
        animateResponseText();

        const finish = () => {
          if (!finished) {
            finished = true;
            options.onFinish(responseText + remainText);
          }
        };

        controller.signal.onabort = finish;

        fetchEventSource(chatPath, {
          ...chatPayload,
          async onopen(res) {
            clearTimeout(requestTimeoutId);
            const contentType = res.headers.get("content-type");
            console.log("[Baidu] request response content type: ", contentType);

            if (contentType?.startsWith("text/plain")) {
              responseText = await res.clone().text();
              return finish();
            }

            if (
              !res.ok ||
              !res.headers
                .get("content-type")
                ?.startsWith(EventStreamContentType) ||
              res.status !== 200
            ) {
              const responseTexts = [responseText];
              let extraInfo = await res.clone().text();
              try {
                const resJson = await res.clone().json();
                extraInfo = prettyObject(resJson);
              } catch { }

              if (res.status === 401) {
                responseTexts.push(Locale.Error.Unauthorized);
              }

              if (extraInfo) {
                responseTexts.push(extraInfo);
              }

              responseText = responseTexts.join("\n\n");

              return finish();
            }
          },
          onmessage(msg) {
            if (msg.data === "[DONE]" || finished) {
              return finish();
            }
            const text = msg.data;
            try {
              const json = JSON.parse(text);
              const choices = json.choices as Array<{
                delta: { content: string };
              }>;
              const delta = choices[0]?.delta?.content;
              if (delta) {
                remainText += delta;
              }
            } catch (e) {
              console.error("[Request] parse error", text, msg);
            }
          },
          async onclose() {
            finish();
            const session = useChatStore.getState().sessions[0];
            const data = {
              id: session.id,
              messages: session.messages.map(item => {
                return {
                  id: item.id,
                  date: item.date,
                  role: item.role,
                  content: item.content,
                }
              })
            }
            await fetch('/api/bigModel', {
              method: 'POST',
              body: JSON.stringify(data)
            });
          },
          onerror(e) {
            options.onError?.(e);
            throw e;
          },
          openWhenHidden: true,
        });
      } else {
        const res = await fetch(chatPath, chatPayload);
        clearTimeout(requestTimeoutId);
        const resJson = await res.json();
        const message = resJson?.result;
        options.onFinish(message);
      }
    } catch (e) {
      options.onError?.(e as Error);
    }
  }
  async usage() {
    return {
      used: 0,
      total: 0,
    };
  }

  async models(): Promise<LLMModel[]> {
    return [];
  }
}