import { Z as ensureMigrated, M as MigrationError, Y as ensureHomeDirs, _ as ensureOpenCodeConfig, $ as ensureOpenCodeSystemConfig, a1 as ensureSecrets, p as checkDocker, aB as performUpgrade, al as isPrerelease, P as PLATFORM_VERSION, H as createLogger } from './src-CidJbygh.js';
import { g as getState } from './state-fxN45gBp.js';
import { d as getRequestId, r as requireAdmin, e as errorResponse, k as jsonResponse } from './helpers-kzwksNq1.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/upgrade/+server.ts
var logger = createLogger("upgrade");
var POST = async (event) => {
	const requestId = getRequestId(event);
	logger.info("upgrade request received", { requestId });
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	try {
		const report = ensureMigrated();
		if (report.migrated) logger.info("layout migrated", {
			requestId,
			from: report.from,
			to: report.to,
			backupDir: report.backupDir,
			notes: report.notes
		});
	} catch (e) {
		if (e instanceof MigrationError) {
			logger.error("auto-migration aborted", {
				requestId,
				error: e.message,
				backupDir: e.backupDir
			});
			return errorResponse(500, "migration_failed", e.message, {
				guidance: e.guidance,
				backupDir: e.backupDir
			}, requestId);
		}
		throw e;
	}
	const state = getState();
	ensureHomeDirs();
	ensureOpenCodeConfig();
	ensureOpenCodeSystemConfig();
	ensureSecrets(state);
	const dockerCheck = await checkDocker();
	if (!dockerCheck.ok) {
		logger.error("upgrade aborted: docker unavailable", {
			requestId,
			stderr: dockerCheck.stderr
		});
		return errorResponse(503, "docker_unavailable", "Docker is not available", { stderr: dockerCheck.stderr }, requestId);
	}
	let result;
	try {
		result = await performUpgrade(state, { allowPrerelease: isPrerelease(PLATFORM_VERSION) });
	} catch (e) {
		const msg = e instanceof Error ? e.message : String(e);
		logger.error("upgrade failed", {
			requestId,
			error: msg
		});
		return errorResponse(502, "upgrade_failed", msg, { message: msg }, requestId);
	}
	logger.info("upgrade completed", {
		requestId,
		imageTag: result.imageTag,
		assetsUpdated: result.assetsUpdated
	});
	return jsonResponse(200, {
		ok: true,
		imageTag: result.imageTag,
		backupDir: result.backupDir,
		assetsUpdated: result.assetsUpdated,
		restarted: result.restarted
	}, requestId);
};

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