import { b3 as summarizeBackups, ap as listBackupDirs, aC as pruneBackupDirs, 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/backups/+server.ts
var logger = createLogger("backups-admin");
/**
* #499 — backup visibility. Returns count, total size, last-backup time and a
* per-backup list (newest first) so the UI can surface the recovery net.
* Read-only.
*/
var GET = async (event) => {
	const requestId = getRequestId(event);
	const authError = requireAdmin(event, requestId);
	if (authError) return authError;
	const state = getState();
	try {
		return jsonResponse(200, {
			ok: true,
			...summarizeBackups(state.homeDir)
		}, requestId);
	} catch (e) {
		return errorResponse(500, "backups_list_failed", e instanceof Error ? e.message : String(e), {}, requestId);
	}
};
/**
* #499 — drives the EXISTING confirm-gated prune. The UI must pass an explicit
* `keep` count (the confirmation is the UI's modal); this NEVER auto-prunes and
* keeps the newest `keep` snapshots, deleting only the older ones.
*/
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 keep = Number(body.keep);
	if (!Number.isInteger(keep) || keep < 0) return errorResponse(400, "invalid_keep", "`keep` must be a non-negative integer", {}, requestId);
	const state = getState();
	try {
		if (listBackupDirs(state.homeDir).slice(keep).length === 0) return jsonResponse(200, {
			ok: true,
			deleted: [],
			kept: keep
		}, requestId);
		const deleted = pruneBackupDirs(state.homeDir, keep);
		logger.info("pruned backups via admin", {
			requestId,
			deleted: deleted.length,
			kept: keep
		});
		return jsonResponse(200, {
			ok: true,
			deleted,
			kept: keep
		}, requestId);
	} catch (e) {
		return errorResponse(500, "backups_prune_failed", e instanceof Error ? e.message : String(e), {}, requestId);
	}
};

export { GET, POST };
//# sourceMappingURL=_server.ts-BWRYvzwI.js.map
