import { NextResponse } from "next/server";
import { getAuditStats, queryAuditEntries } from "@omniroute/open-sse/mcp-server/audit";
import {
  isMcpHeartbeatOnline,
  isProcessAlive,
  readMcpHeartbeat,
  resolveMcpHeartbeatPath,
} from "@omniroute/open-sse/mcp-server/runtimeHeartbeat";
import { getMcpHttpStatus } from "../../../../../open-sse/mcp-server/httpTransport";
import { getSettings } from "@/lib/db/settings";

export async function GET() {
  try {
    const [heartbeat, stats, lastCallPage, settings] = await Promise.all([
      readMcpHeartbeat(),
      getAuditStats(),
      queryAuditEntries({ limit: 1, offset: 0 }),
      getSettings(),
    ]);

    const mcpEnabled = !!settings.mcpEnabled;
    const mcpTransport = (settings.mcpTransport as string) || "stdio";

    // Check HTTP transport (SSE / Streamable HTTP) if active
    const httpStatus = getMcpHttpStatus();

    // stdio uses heartbeat file; HTTP transports use in-process state
    const stdioOnline = isMcpHeartbeatOnline(heartbeat, { requireLivePid: true });
    const online = mcpTransport === "stdio" ? stdioOnline : httpStatus.online;

    const lastCall = lastCallPage.entries[0] || null;
    const now = Date.now();
    const lastHeartbeatAtMs = heartbeat ? new Date(heartbeat.lastHeartbeatAt).getTime() : null;
    const startedAtMs = heartbeat ? new Date(heartbeat.startedAt).getTime() : null;
    const heartbeatAgeMs =
      typeof lastHeartbeatAtMs === "number" && Number.isFinite(lastHeartbeatAtMs)
        ? Math.max(0, now - lastHeartbeatAtMs)
        : null;
    const uptimeMs =
      typeof startedAtMs === "number" && Number.isFinite(startedAtMs)
        ? Math.max(0, now - startedAtMs)
        : null;

    return NextResponse.json({
      status: online ? "online" : "offline",
      online,
      enabled: mcpEnabled,
      transport: mcpTransport,
      heartbeatPath: resolveMcpHeartbeatPath(),
      heartbeat: heartbeat
        ? {
            ...heartbeat,
            pidAlive: isProcessAlive(heartbeat.pid),
            heartbeatAgeMs,
            uptimeMs,
          }
        : null,
      httpTransport: httpStatus,
      activity: {
        totalCalls24h: stats.totalCalls,
        successRate: stats.successRate,
        avgDurationMs: stats.avgDurationMs,
        topTools: stats.topTools,
        lastCallAt: lastCall?.createdAt || null,
        lastCallTool: lastCall?.toolName || null,
      },
    });
  } catch (error) {
    const message = error instanceof Error ? error.message : "Failed to load MCP status";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
