import { getLogger as getLogger$1 } from "../../../logger.js";
import { env } from "../../../env.js";
import { enforceSecurityFilters } from "../../../utils/signozHelpers.js";
import { canViewProject, createApiError } from "@inkeep/agents-core";
import { Hono } from "hono";
import axios from "axios";

//#region src/domains/manage/routes/signoz.ts
const logger = getLogger$1("signoz-proxy");
const app = new Hono();
app.post("/query", async (c) => {
	let payload = await c.req.json();
	const requestedProjectId = payload.projectId;
	const tenantId = c.get("tenantId");
	const userId = c.get("userId");
	const tenantRole = c.get("tenantRole");
	if (!userId || !tenantId) throw createApiError({
		code: "unauthorized",
		message: "User or organization context not found",
		instance: c.req.path
	});
	logger.debug({
		tenantId,
		projectId: requestedProjectId,
		hasProjectId: !!requestedProjectId
	}, "Processing SigNoz query request");
	if (requestedProjectId) {
		if (!(userId === "system" || userId?.startsWith("apikey:"))) {
			if (!await canViewProject({
				userId,
				projectId: requestedProjectId,
				orgRole: tenantRole
			})) {
				logger.warn({
					tenantId,
					projectId: requestedProjectId,
					userId
				}, "Project not found or access denied");
				return c.json({
					error: "Forbidden",
					message: "You do not have access to this project"
				}, 403);
			}
		}
	}
	payload = enforceSecurityFilters(payload, tenantId, requestedProjectId);
	logger.debug({
		tenantId,
		projectId: requestedProjectId
	}, "Security filters enforced");
	const signozUrl = env.SIGNOZ_URL || env.PUBLIC_SIGNOZ_URL;
	const signozApiKey = env.SIGNOZ_API_KEY;
	if (!signozUrl || !signozApiKey) {
		logger.error({}, "SigNoz not configured");
		return c.json({
			error: "Service Unavailable",
			message: "SigNoz is not configured"
		}, 500);
	}
	try {
		const signozEndpoint = `${signozUrl}/api/v4/query_range`;
		logger.debug({ endpoint: signozEndpoint }, "Proxying to SigNoz");
		const response = await axios.post(signozEndpoint, payload, {
			headers: {
				"Content-Type": "application/json",
				"SIGNOZ-API-KEY": signozApiKey
			},
			timeout: 3e4
		});
		logger.debug({ status: response.status }, "SigNoz query successful");
		return c.json(response.data);
	} catch (error) {
		if (axios.isAxiosError(error)) {
			if (error.code === "ECONNREFUSED" || error.code === "ENOTFOUND") {
				logger.error({ error: error.message }, "SigNoz service unavailable");
				return c.json({
					error: "Service Unavailable",
					message: "SigNoz service is unavailable"
				}, 503);
			}
			if (error.response?.status === 401 || error.response?.status === 403) {
				logger.error({ status: error.response.status }, "SigNoz authentication failed");
				return c.json({
					error: "Internal Server Error",
					message: "SigNoz authentication failed"
				}, 500);
			}
			if (error.response?.status === 400) {
				logger.warn({ status: error.response.status }, "Invalid SigNoz query");
				return c.json({
					error: "Bad Request",
					message: "Invalid query parameters"
				}, 400);
			}
		}
		logger.error({ error }, "SigNoz query failed");
		return c.json({
			error: "Internal Server Error",
			message: "Failed to query SigNoz"
		}, 500);
	}
});
app.get("/health", async (c) => {
	const signozUrl = env.SIGNOZ_URL || env.PUBLIC_SIGNOZ_URL;
	const signozApiKey = env.SIGNOZ_API_KEY;
	logger.debug({
		hasUrl: !!signozUrl,
		hasApiKey: !!signozApiKey,
		url: signozUrl
	}, "Checking SigNoz configuration");
	if (!signozUrl || !signozApiKey) {
		logger.warn({}, "SigNoz credentials not set");
		return c.json({
			status: "not_configured",
			configured: false,
			error: "SIGNOZ_URL or SIGNOZ_API_KEY not set"
		});
	}
	try {
		const testPayload = {
			start: Date.now() - 3e5,
			end: Date.now(),
			step: 60,
			compositeQuery: {
				queryType: "builder",
				panelType: "table",
				builderQueries: {}
			}
		};
		const signozEndpoint = `${signozUrl}/api/v4/query_range`;
		logger.debug({ endpoint: signozEndpoint }, "Testing SigNoz connection");
		await axios.post(signozEndpoint, testPayload, {
			headers: {
				"Content-Type": "application/json",
				"SIGNOZ-API-KEY": signozApiKey
			},
			timeout: 5e3,
			validateStatus: (status) => status === 200 || status === 400
		});
		logger.debug({}, "SigNoz health check successful");
		return c.json({
			status: "ok",
			configured: true
		});
	} catch (error) {
		logger.error({
			error,
			message: error instanceof Error ? error.message : "Unknown error",
			code: axios.isAxiosError(error) ? error.code : void 0,
			status: axios.isAxiosError(error) ? error.response?.status : void 0
		}, "SigNoz connection test failed");
		let errorMessage = "Failed to connect to SigNoz";
		if (axios.isAxiosError(error)) {
			if (error.code === "ECONNREFUSED" || error.code === "ENOTFOUND") errorMessage = "Check SIGNOZ_URL";
			else if (error.response?.status === 401 || error.response?.status === 403) errorMessage = "Invalid SIGNOZ_API_KEY";
			else if (error.code === "ETIMEDOUT" || error.code === "ECONNABORTED") errorMessage = "SigNoz connection timed out";
		}
		return c.json({
			status: "connection_failed",
			configured: false,
			error: errorMessage
		});
	}
});
var signoz_default = app;

//#endregion
export { signoz_default as default };