import manageDbPool_default from "../data/db/manageDbPool.js";
import runDbClient_default from "../data/db/runDbClient.js";
import { checkManageDb, checkRunDb } from "../utils/healthChecks.js";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { createProtectedRoute, noAuth } from "@inkeep/agents-core/middleware";

//#region src/routes/healthChecks.ts
const healthChecksHandler = new OpenAPIHono();
healthChecksHandler.openapi(createProtectedRoute({
	method: "get",
	path: "/health",
	operationId: "health",
	tags: ["Health"],
	summary: "Health check",
	description: "Check if the management service is healthy",
	permission: noAuth(),
	responses: { 204: { description: "Service is healthy" } }
}), (c) => {
	return c.body(null, 204);
});
const ReadyResponseSchema = z.object({
	status: z.literal("ok"),
	manageDb: z.boolean().describe("Whether the manage database is reachable"),
	runDb: z.boolean().describe("Whether the run database is reachable")
}).openapi("ReadyResponse");
const ReadyErrorChecksSchema = z.object({
	manageDb: z.boolean().describe("Whether the manage database check passed"),
	runDb: z.boolean().describe("Whether the run database check passed")
}).openapi("ReadyErrorChecks");
const ReadyErrorResponseSchema = z.object({
	type: z.string().describe("A URI reference that identifies the problem type"),
	title: z.string().describe("A short, human-readable summary of the problem type"),
	status: z.number().describe("The HTTP status code"),
	detail: z.string().describe("A human-readable explanation specific to this occurrence"),
	checks: ReadyErrorChecksSchema
}).openapi("ReadyErrorResponse");
healthChecksHandler.openapi(createProtectedRoute({
	method: "get",
	path: "/ready",
	operationId: "ready",
	tags: ["Health"],
	summary: "Readiness check",
	description: "Check if the service is ready to serve traffic by verifying database connectivity",
	permission: noAuth(),
	responses: {
		200: {
			description: "Service is ready - all health checks passed",
			content: { "application/json": { schema: ReadyResponseSchema } }
		},
		503: {
			description: "Service is not ready - one or more health checks failed",
			content: { "application/problem+json": { schema: ReadyErrorResponseSchema } }
		}
	}
}), async (c) => {
	const [manageDbHealthy, runDbHealthy] = await Promise.all([checkManageDb(manageDbPool_default), checkRunDb(runDbClient_default)]);
	if (manageDbHealthy && runDbHealthy) return c.json({
		status: "ok",
		manageDb: true,
		runDb: true
	});
	const failedChecks = [];
	if (!manageDbHealthy) failedChecks.push("manage database");
	if (!runDbHealthy) failedChecks.push("run database");
	return c.json({
		type: "https://httpstatuses.com/503",
		title: "Service Unavailable",
		status: 503,
		detail: `Health checks failed: ${failedChecks.join(", ")}`,
		checks: {
			manageDb: manageDbHealthy,
			runDb: runDbHealthy
		}
	}, 503, { "Content-Type": "application/problem+json" });
});

//#endregion
export { healthChecksHandler };