import { LOG_DIR, getLogFiles, getLogLevels, getLogModules, getLogStats, queryLogs } from "../../../utils/logger/log-store.js";
//#region src/gateway/hono/routes/logs.ts
function registerLogsRoutes(authenticated, _deps) {
	authenticated.get("/api/logs", async (c) => {
		const query = c.req.query();
		const logs = await queryLogs({
			levels: query.level ? query.level.split(",") : void 0,
			from: query.from,
			to: query.to,
			q: query.q,
			module: query.module,
			limit: query.limit ? parseInt(query.limit) : 100,
			offset: query.offset ? parseInt(query.offset) : 0
		});
		return c.json({
			logs,
			count: logs.length
		});
	});
	authenticated.get("/api/logs/files", async (c) => {
		const files = getLogFiles();
		return c.json({ files });
	});
	authenticated.get("/api/logs/stats", async (c) => {
		const stats = getLogStats();
		return c.json(stats);
	});
	authenticated.get("/api/logs/levels", async (c) => {
		return c.json({ levels: getLogLevels() });
	});
	authenticated.get("/api/logs/modules", async (c) => {
		const modules = await getLogModules();
		return c.json({ modules });
	});
	authenticated.get("/api/logs/dir", async (c) => {
		return c.json({ dir: LOG_DIR });
	});
	authenticated.get("/api/logs/health", async (c) => {
		const { getLogDir, getLogStats, isLoggerShuttingDown } = await import("../../../utils/logger.js");
		const { getLogFiles } = await import("../../../utils/logger/log-store.js");
		const stats = getLogStats();
		const files = getLogFiles().slice(0, 5);
		const isShuttingDown = isLoggerShuttingDown();
		return c.json({
			status: isShuttingDown ? "shutting_down" : "healthy",
			config: {
				dir: getLogDir(),
				uptimeMs: stats.uptimeMs
			},
			stats: {
				byLevel: stats.byLevel,
				errorsLast24h: stats.errorsLast24h,
				modulesTracked: stats.byModule ? Object.keys(stats.byModule).length : 0
			},
			files: files.map((f) => ({
				name: f.name,
				size: f.size,
				modified: f.modified,
				type: f.type
			})),
			shuttingDown: isShuttingDown
		});
	});
	authenticated.post("/api/logs/level", async (c) => {
		const { setLogLevel, getLogLevel } = await import("../../../utils/logger.js");
		const { level, duration } = await c.req.json().catch(() => ({}));
		if (!level) return c.json({ error: "level is required" }, 400);
		const validLevels = [
			"trace",
			"debug",
			"info",
			"warn",
			"error",
			"fatal"
		];
		if (!validLevels.includes(level)) return c.json({ error: `Invalid level. Must be one of: ${validLevels.join(", ")}` }, 400);
		const previousLevel = getLogLevel();
		setLogLevel(level);
		let autoRevertAt = null;
		if (duration) {
			const durationMs = parseInt(duration) * 6e4;
			if (!isNaN(durationMs) && durationMs > 0) {
				autoRevertAt = new Date(Date.now() + durationMs).toISOString();
				setTimeout(() => {
					setLogLevel(previousLevel);
					console.log(`[Logger] Auto-reverted log level to ${previousLevel}`);
				}, durationMs);
			}
		}
		return c.json({
			previous: previousLevel,
			current: level,
			autoRevertAt,
			message: `Log level changed from ${previousLevel} to ${level}`
		});
	});
	authenticated.get("/api/logs/level", async (c) => {
		const { getLogLevel } = await import("../../../utils/logger.js");
		return c.json({ level: getLogLevel() });
	});
	authenticated.get("/api/logs/stream", async (c) => {
		const { createLogSSEHandler } = await import("../../../utils/logger/log-stream.js");
		return createLogSSEHandler()(c);
	});
}
//#endregion
export { registerLogsRoutes };

//# sourceMappingURL=logs.js.map