import { init_loader, loadConfig } from "../../../../config/loader.js";
import { resolveGatewayService } from "../../../../daemon/service.js";
import { existsSync } from "node:fs";
//#region src/cli/commands/doctor/checks/gateway-service.ts
init_loader();
async function checkGatewayService(ctx) {
	if (!existsSync(ctx.configPath)) return {
		id: "gateway-service",
		label: "Gateway service",
		status: "skip",
		message: "No config file; skipped.",
		hints: []
	};
	try {
		loadConfig(ctx.configPath);
	} catch {
		return {
			id: "gateway-service",
			label: "Gateway service",
			status: "skip",
			message: "Config could not be loaded; skipped.",
			hints: []
		};
	}
	let service;
	try {
		service = await resolveGatewayService();
	} catch (e) {
		return {
			id: "gateway-service",
			label: "Gateway service",
			status: "skip",
			message: `Service backend unavailable: ${e instanceof Error ? e.message : String(e)}`,
			hints: []
		};
	}
	const env = { ...process.env };
	if (!await service.isLoaded({ env })) return {
		id: "gateway-service",
		label: "Gateway service",
		status: "warn",
		message: "Gateway is not installed as a system service.",
		hints: ["Install: xopc gateway service install"]
	};
	const runtime = await service.readRuntime(env);
	if (runtime.status === "running" && runtime.pid) return {
		id: "gateway-service",
		label: "Gateway service",
		status: "pass",
		message: `Gateway service is running (PID ${runtime.pid}).`,
		hints: [service.label]
	};
	return {
		id: "gateway-service",
		label: "Gateway service",
		status: "warn",
		message: `Gateway service is installed but not running (status: ${runtime.status}).`,
		hints: ["Start: xopc gateway service start"]
	};
}
//#endregion
export { checkGatewayService };

//# sourceMappingURL=gateway-service.js.map