import { aj as isDeployableUnit, p as checkDocker, f as applyUnitImageTagChange, d as applyTagChange, D as DowngradeConfirmationRequired, 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/stack-version/+server.ts
var logger = createLogger("stack-version");
var PATCH = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	let body;
	try {
		body = await event.request.json();
	} catch {
		return errorResponse(400, "invalid_json", "Request body must be valid JSON", {}, requestId);
	}
	const tag = typeof body.tag === "string" ? body.tag.trim() : "";
	if (!tag) return errorResponse(400, "tag_required", "tag is required", {}, requestId);
	if (!/^[a-zA-Z0-9._\-]+$/.test(tag)) return errorResponse(400, "invalid_tag", "Tag must be alphanumeric with . _ or - only", {}, requestId);
	const confirmDowngrade = body.confirmDowngrade === true;
	const unit = typeof body.unit === "string" ? body.unit.trim() : "";
	if (unit && !isDeployableUnit(unit)) return errorResponse(400, "invalid_unit", `Unknown deployable unit: ${unit}`, {}, requestId);
	const state = getState();
	const dockerCheck = await checkDocker();
	if (!dockerCheck.ok) {
		logger.error("stack-version aborted: docker unavailable", { requestId });
		return errorResponse(503, "docker_unavailable", "Docker is not available", { stderr: dockerCheck.stderr }, requestId);
	}
	let result;
	try {
		result = unit ? await applyUnitImageTagChange(state, unit, tag, { confirmDowngrade }) : await applyTagChange(state, tag, { confirmDowngrade });
	} catch (e) {
		if (e instanceof DowngradeConfirmationRequired) {
			logger.info("stack-version downgrade requires confirmation", {
				requestId,
				unit: unit || "stack",
				currentVersion: e.currentVersion,
				targetVersion: e.targetVersion
			});
			return errorResponse(409, "downgrade_confirmation_required", e.message, {
				currentVersion: e.currentVersion,
				targetVersion: e.targetVersion
			}, requestId);
		}
		const msg = e instanceof Error ? e.message : String(e);
		logger.error("stack-version apply failed", {
			requestId,
			unit: unit || "stack",
			error: msg
		});
		return errorResponse(502, "apply_failed", msg, { message: msg }, requestId);
	}
	logger.info("stack-version applied", {
		requestId,
		unit: unit || "stack",
		imageTag: result.imageTag
	});
	return jsonResponse(200, {
		ok: true,
		imageTag: result.imageTag,
		restarted: result.restarted
	}, requestId);
};

export { PATCH };
//# sourceMappingURL=_server.ts-Bz06ow3I.js.map
