import { readBody } from "h3";
import { createUIMessageStream, createUIMessageStreamResponse } from "ai";
import { getAuthContext } from "../utils/auth";
import { prisma } from "../utils/prisma";
import { buildChangeSet, captureSnapshot } from "../utils/changeSet";
import { persistChatMessage, runCrmAgentFor, type AgentTraceEvent } from "../agent/crmAgent";

function extractMessageText(message: any): string {
  if (!message || !Array.isArray(message.parts)) return "";
  return message.parts
    .filter((part: any) => part?.type === "text" && typeof part.text === "string")
    .map((part: any) => part.text)
    .join("")
    .trim();
}

function getLastUserText(messages: any[]): string {
  for (let i = messages.length - 1; i >= 0; i -= 1) {
    const message = messages[i];
    if (message?.role !== "user") continue;
    const text = extractMessageText(message);
    if (text) return text;
  }
  return "";
}

function humanizeTraceText(trace: AgentTraceEvent): string {
  if (trace.toolRun?.name) {
    return `Использую инструмент: ${trace.toolRun.name}`;
  }

  const text = (trace.text ?? "").trim();
  if (!text) return "Агент работает с данными CRM.";

  if (text.toLowerCase().includes("ошиб")) return "Возникла ошибка шага, пробую другой путь.";
  if (text.toLowerCase().includes("итог")) return "Готовлю финальный ответ.";
  return text;
}

export default defineEventHandler(async (event) => {
  const auth = await getAuthContext(event);
  const body = await readBody<{ messages?: any[] }>(event);
  const messages = Array.isArray(body?.messages) ? body.messages : [];
  const userText = getLastUserText(messages);

  if (!userText) {
    throw createError({ statusCode: 400, statusMessage: "Last user message is required" });
  }
  const requestId = `req_${Date.now()}_${Math.floor(Math.random() * 1_000_000)}`;

  const stream = createUIMessageStream({
    execute: async ({ writer }) => {
      const textId = `text-${Date.now()}`;
      writer.write({ type: "start" });
      try {
        const snapshotBefore = await captureSnapshot(prisma, auth.teamId);

        await persistChatMessage({
          teamId: auth.teamId,
          conversationId: auth.conversationId,
          authorUserId: auth.userId,
          role: "USER",
          text: userText,
          requestId,
          eventType: "user",
          phase: "final",
          transient: false,
        });

        const reply = await runCrmAgentFor({
          teamId: auth.teamId,
          userId: auth.userId,
          userText,
          requestId,
          conversationId: auth.conversationId,
          onTrace: async (trace: AgentTraceEvent) => {
            writer.write({
              type: "data-agent-log",
              data: {
                requestId,
                at: new Date().toISOString(),
                text: humanizeTraceText(trace),
              },
            });
          },
        });

        const snapshotAfter = await captureSnapshot(prisma, auth.teamId);
        const changeSet = buildChangeSet(snapshotBefore, snapshotAfter);

        await persistChatMessage({
          teamId: auth.teamId,
          conversationId: auth.conversationId,
          authorUserId: null,
          role: "ASSISTANT",
          text: reply.text,
          requestId,
          eventType: "assistant",
          phase: "final",
          transient: false,
          changeSet,
        });

        writer.write({ type: "text-start", id: textId });
        writer.write({ type: "text-delta", id: textId, delta: reply.text });
        writer.write({ type: "text-end", id: textId });
        writer.write({ type: "finish", finishReason: "stop" });
      } catch (error: any) {
        writer.write({
          type: "data-agent-log",
          data: {
            requestId,
            at: new Date().toISOString(),
            text: "Ошибка выполнения агентского цикла.",
          },
        });
        writer.write({ type: "text-start", id: textId });
        writer.write({
          type: "text-delta",
          id: textId,
          delta: `Не удалось завершить задачу: ${String(error?.message ?? "unknown error")}`,
        });
        writer.write({ type: "text-end", id: textId });
        writer.write({ type: "finish", finishReason: "stop" });
      }
    },
  });

  return createUIMessageStreamResponse({ stream });
});
