import { performance } from "node:perf_hooks";
import { prisma } from "../db.js";
import { anthropicClient, hermesAgentClient, openaiClient, xaiClient } from "./providers.js";
import { buildToolLogMessageData, runPlainChatCompletions, runToolAwareChatCompletions, runToolAwareOpenAIChat } from "./chat-tools.js";
import { buildAnthropicConversationMessage, getAnthropicSystemPrompt } from "./message-content.js";
import { toPrismaProvider } from "./provider-ids.js";
import type { MultiplexRequest, MultiplexResponse, Provider } from "./types.js";

function asProviderEnum(p: Provider) {
  return toPrismaProvider(p);
}

export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResponse> {
  const t0 = performance.now();
  const chatId = req.chatId ?? (await prisma.chat.create({ data: {}, select: { id: true } })).id;

  // Persist call record early so we can attach errors.
  const call = await prisma.llmCall.create({
    data: {
      chatId,
      provider: asProviderEnum(req.provider) as any,
      model: req.model,
      request: req as any,
    },
    select: { id: true, chatId: true },
  });

  await prisma.$transaction([
    prisma.chat.update({
      where: { id: chatId },
      data: {
        lastUsedProvider: asProviderEnum(req.provider) as any,
        lastUsedModel: req.model,
      },
    }),
    prisma.chat.updateMany({
      where: { id: chatId, initiatedProvider: null },
      data: {
        initiatedProvider: asProviderEnum(req.provider) as any,
        initiatedModel: req.model,
      },
    }),
  ]);

  try {
    let outText = "";
    let usage: MultiplexResponse["usage"] | undefined;
    let raw: unknown;
    let toolMessages: ReturnType<typeof buildToolLogMessageData>[] = [];

    if (req.provider === "openai") {
      const client = openaiClient();
      const r = await runToolAwareOpenAIChat({
        client,
        model: req.model,
        messages: req.messages,
        temperature: req.temperature,
        maxTokens: req.maxTokens,
        logContext: {
          provider: req.provider,
          model: req.model,
          chatId,
        },
      });
      raw = r.raw;
      outText = r.text;
      usage = r.usage;
      toolMessages = r.toolEvents.map((event) => buildToolLogMessageData(call.chatId, event));
    } else if (req.provider === "xai") {
      const client = xaiClient();
      const r = await runToolAwareChatCompletions({
        client,
        model: req.model,
        messages: req.messages,
        temperature: req.temperature,
        maxTokens: req.maxTokens,
        logContext: {
          provider: req.provider,
          model: req.model,
          chatId,
        },
      });
      raw = r.raw;
      outText = r.text;
      usage = r.usage;
      toolMessages = r.toolEvents.map((event) => buildToolLogMessageData(call.chatId, event));
    } else if (req.provider === "hermes-agent") {
      const client = hermesAgentClient();
      const r = await runPlainChatCompletions({
        client,
        model: req.model,
        messages: req.messages,
        temperature: req.temperature,
        maxTokens: req.maxTokens,
        logContext: {
          provider: req.provider,
          model: req.model,
          chatId,
        },
      });
      raw = r.raw;
      outText = r.text;
      usage = r.usage;
    } else if (req.provider === "anthropic") {
      const client = anthropicClient();

      const system = getAnthropicSystemPrompt(req.messages);
      const msgs = req.messages.filter((message) => message.role !== "system").map((message) => buildAnthropicConversationMessage(message));

      const r = await client.messages.create({
        model: req.model,
        system,
        max_tokens: req.maxTokens ?? 1024,
        temperature: req.temperature,
        messages: msgs as any,
      });
      raw = r;
      outText = r.content
        .map((c: any) => (c.type === "text" ? c.text : ""))
        .join("")
        .trim();

      // Anthropic usage (SDK typing varies by version)
      const ru: any = (r as any).usage;
      if (ru) {
        usage = {
          inputTokens: ru.input_tokens,
          outputTokens: ru.output_tokens,
          totalTokens: (ru.input_tokens ?? 0) + (ru.output_tokens ?? 0),
        };
      }
    } else {
      throw new Error(`unknown provider: ${req.provider}`);
    }

    const latencyMs = Math.round(performance.now() - t0);

    // Store tool activity (if any), assistant message, and call record.
    await prisma.$transaction(async (tx) => {
      if (toolMessages.length) {
        await tx.message.createMany({
          data: toolMessages.map((message) => ({
            chatId: message.chatId,
            role: message.role as any,
            content: message.content,
            name: message.name,
            metadata: message.metadata as any,
          })),
        });
      }
      await tx.message.create({
        data: {
          chatId: call.chatId,
          role: "assistant" as any,
          content: outText,
        },
      });
      await tx.llmCall.update({
        where: { id: call.id },
        data: {
          response: raw as any,
          latencyMs,
          inputTokens: usage?.inputTokens,
          outputTokens: usage?.outputTokens,
          totalTokens: usage?.totalTokens,
        },
      });
    });

    return {
      provider: req.provider,
      model: req.model,
      message: { role: "assistant", content: outText },
      usage,
      raw,
    };
  } catch (e: any) {
    const latencyMs = Math.round(performance.now() - t0);
    await prisma.llmCall.update({
      where: { id: call.id },
      data: {
        error: e?.message ?? String(e),
        latencyMs,
      },
    });
    throw e;
  }
}
