import { H as createLogger, Y as ensureHomeDirs, _ as ensureOpenCodeConfig, $ as ensureOpenCodeSystemConfig, a1 as ensureSecrets, c as applyInstall, o as buildManagedServices, p as checkDocker, G as composeUp, n as buildComposeOptions, C as CORE_SERVICES } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, k as jsonResponse, e as errorResponse } from './helpers-kzwksNq1.js';
import { w as withSerialQueue } from './serial-queue-D9FEpYVv.js';
import './chunk-CLZ62Ad-.js';
import 'node:module';
import './provider-constants-DPrj3yBX.js';
import './dist-DOFcG0Zp.js';
import 'node:fs';
import 'node:path';
import 'buffer';
import 'fs';
import 'path';
import 'node:child_process';
import 'node:url';
import 'node:crypto';
import 'events';
import 'node:events';
import 'node:stream';
import 'node:string_decoder';
import 'assert';
import 'zlib';
import 'node:assert';
import 'node:fs/promises';
import 'node:os';
import './endpoints-UdQGgKsR.js';

//#region src/routes/admin/install/+server.ts
var logger = createLogger("install");
var POST = async (event) => {
	const requestId = getRequestId(event);
	logger.info("install request received", { requestId });
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	return withSerialQueue("admin:install", async () => {
		try {
			const state = getState();
			logger.info("ensuring home directories and seeding config", { requestId });
			ensureHomeDirs();
			ensureOpenCodeConfig();
			ensureOpenCodeSystemConfig();
			ensureSecrets(state);
			await applyInstall(state);
			const managedServices = await buildManagedServices(state);
			logger.info("checking Docker availability", { requestId });
			const dockerCheck = await checkDocker();
			let dockerResult = null;
			if (dockerCheck.ok) {
				logger.info("starting compose up", {
					requestId,
					services: managedServices
				});
				dockerResult = await composeUp({
					...buildComposeOptions(state),
					services: managedServices
				});
			}
			const started = [...CORE_SERVICES];
			logger.info("install completed", {
				requestId,
				started,
				dockerAvailable: dockerCheck.ok,
				composeOk: dockerResult?.ok ?? null
			});
			return jsonResponse(200, {
				ok: true,
				started,
				dockerAvailable: dockerCheck.ok,
				composeResult: dockerResult ? {
					ok: dockerResult.ok,
					stderr: dockerResult.stderr
				} : null
			}, requestId);
		} catch (err) {
			const msg = err instanceof Error ? err.message : String(err);
			logger.error("install failed", {
				requestId,
				error: msg
			});
			return errorResponse(500, "install_failed", msg, {}, requestId);
		}
	});
};

export { POST };
//# sourceMappingURL=_server.ts-CMuzkwXb.js.map
