import { Z as ensureMigrated, B as BackupSpaceError, M as MigrationError, H as createLogger } 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 './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/migrate-apply/+server.ts
var logger = createLogger("migrate-apply");
/**
* Apply the pending on-disk migration and report the result. This is the
* lightweight counterpart to /admin/migrate-preview: it runs the REAL
* `ensureMigrated()` (layout migrations + release migrations up to the home's
* recorded version), which takes a full-home backup first and is idempotent.
*
* Unlike /admin/upgrade this does NOT pull images or recreate containers — it
* only reconciles the home directory so the splash "invalid state" landing can
* unblock a user whose home needs migrating, then route them onward.
*
* BackupSpaceError (the pre-backup free-space guard) is surfaced as 409 with the
* estimate so the UI can ask the user to confirm before retrying with
* `confirmLowSpace: true`. Nothing is modified when that fires.
*/
var POST = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	let body;
	try {
		body = await event.request.json();
	} catch {
		body = {};
	}
	const confirmLowSpace = body.confirmLowSpace === true;
	const state = getState();
	const lines = [];
	try {
		const report = ensureMigrated({
			homeDir: state.homeDir,
			confirmLowSpace,
			log: (m) => lines.push(m)
		});
		logger.info("migration applied", {
			requestId,
			migrated: report.migrated,
			from: report.from,
			to: report.to,
			releaseApplied: report.releaseApplied,
			backupDir: report.backupDir
		});
		return jsonResponse(200, {
			ok: true,
			migrated: report.migrated,
			from: report.from,
			to: report.to,
			applied: report.applied,
			releaseFrom: report.releaseFrom,
			releaseTo: report.releaseTo,
			releaseApplied: report.releaseApplied,
			backupDir: report.backupDir,
			notes: report.notes,
			lines
		}, requestId);
	} catch (e) {
		if (e instanceof BackupSpaceError) {
			logger.warn("migration blocked: low free space", {
				requestId,
				estimatedBytes: e.estimatedBytes,
				freeBytes: e.freeBytes
			});
			return errorResponse(409, "low_space", e.message, {
				guidance: e.guidance,
				estimatedBytes: e.estimatedBytes,
				freeBytes: e.freeBytes,
				confirmable: true
			}, requestId);
		}
		if (e instanceof MigrationError) {
			logger.error("migration failed", {
				requestId,
				error: e.message,
				backupDir: e.backupDir
			});
			return errorResponse(500, "migration_failed", e.message, {
				guidance: e.guidance,
				backupDir: e.backupDir,
				lines
			}, requestId);
		}
		const msg = e instanceof Error ? e.message : String(e);
		logger.error("migration failed (unexpected)", {
			requestId,
			error: msg
		});
		return errorResponse(500, "migration_failed", msg, { lines }, requestId);
	}
};

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