import { loadEnvironmentFiles } from "../env.js";
import { createAgentsManageDatabaseClient } from "../db/manage/manage-client.js";
import { syncSchemaFromMain } from "./schema-sync.js";
import { doltCheckout, doltListBranches } from "./branch.js";
import { confirmMigration } from "../db/utils.js";

//#region src/dolt/migrate-all-branches.ts
const ansi = {
	reset: "\x1B[0m",
	dim: "\x1B[2m",
	bold: "\x1B[1m",
	green: "\x1B[32m",
	red: "\x1B[31m",
	yellow: "\x1B[33m",
	cyan: "\x1B[36m"
};
const format = {
	header: (text) => `${ansi.bold}${ansi.cyan}${text}${ansi.reset}`,
	ok: (text) => `${ansi.green}${text}${ansi.reset}`,
	warn: (text) => `${ansi.yellow}${text}${ansi.reset}`,
	err: (text) => `${ansi.red}${text}${ansi.reset}`,
	dim: (text) => `${ansi.dim}${text}${ansi.reset}`,
	bold: (text) => `${ansi.bold}${text}${ansi.reset}`
};
const ms = (start, end) => `${Math.max(0, end - start)}ms`;
const main = async () => {
	loadEnvironmentFiles();
	const connectionString = process.env.INKEEP_AGENTS_MANAGE_DATABASE_URL;
	await confirmMigration(connectionString);
	const startedAt = Date.now();
	const db = createAgentsManageDatabaseClient({});
	const branches = await doltListBranches(db)();
	const targetBranches = branches.filter((b) => b.name !== "main");
	const nameWidth = Math.max(12, ...targetBranches.map((b) => b.name.length));
	console.log(format.header(`Sync schema from main → ${targetBranches.length} branches`));
	console.log(format.dim(`Started: ${(/* @__PURE__ */ new Date()).toISOString()}`));
	console.log("");
	let syncedCount = 0;
	let upToDateCount = 0;
	let errorCount = 0;
	for (const branch of branches) {
		if (branch.name === "main") continue;
		const branchStartedAt = Date.now();
		const paddedName = branch.name.padEnd(nameWidth, " ");
		console.log(`${format.bold("─".repeat(Math.min(80, nameWidth + 34)))}`);
		console.log(`${format.bold("Branch")}  ${format.header(paddedName)}`);
		try {
			process.stdout.write(`${format.dim("Action ")}  checkout… `);
			await doltCheckout(db)({ branch: branch.name });
			console.log(format.ok("[OK]"));
			process.stdout.write(`${format.dim("Action ")}  sync schema… `);
			const result = await syncSchemaFromMain(db)({ autoCommitPending: true });
			if (result.synced) {
				syncedCount += 1;
				console.log(format.ok("[SYNCED]"));
				console.log(`${format.dim("Result ")}  ${format.ok("Schema updated")} ${format.dim(`(${ms(branchStartedAt, Date.now())})`)}`);
			} else if (result.error) {
				errorCount += 1;
				console.log(format.err("[FAILED]"));
				console.log(`${format.dim("Result ")}  ${format.err("Error")} ${format.dim(`(${ms(branchStartedAt, Date.now())})`)}`);
				console.log(`${format.dim("Details")}  ${result.error}`);
				break;
			} else {
				upToDateCount += 1;
				console.log(format.warn("[NOOP]"));
				console.log(`${format.dim("Result ")}  ${format.warn("Already up to date")} ${format.dim(`(${ms(branchStartedAt, Date.now())})`)}`);
			}
		} catch (error) {
			errorCount += 1;
			console.log(format.err("[FAILED]"));
			const message = error instanceof Error ? error.message : String(error);
			console.log(`${format.dim("Result ")}  ${format.err("Error")} ${format.dim(`(${ms(branchStartedAt, Date.now())})`)}`);
			console.log(`${format.dim("Details")}  ${message}`);
			break;
		}
		console.log("");
	}
	console.log(format.bold("─".repeat(80)));
	console.log(`${format.bold("Summary")}  ${format.ok(`${syncedCount} synced`)}, ${format.warn(`${upToDateCount} up to date`)}, ${errorCount > 0 ? format.err(`${errorCount} failed`) : format.ok("0 failed")}`);
	console.log(format.dim(`Total: ${ms(startedAt, Date.now())}`));
	if (errorCount > 0) process.exit(1);
};
main();

//#endregion
export { main };