import { env } from "../env.js";
import { createAgentsManageDatabaseClient } from "./manage/manage-client.js";
import { createAgentsRunDatabaseClient } from "./runtime/runtime-client.js";
import { sql } from "drizzle-orm";

//#region src/db/delete.ts
/**
* Drops all tables, sequences, types, and functions from the public schema
* WARNING: This is destructive and cannot be undone!
*/
async function deleteDatabase(type) {
	console.log(`🗑️  Deleting all database objects for environment: ${env.ENVIRONMENT}`);
	console.log("---");
	const dbClient = type === "manage" ? createAgentsManageDatabaseClient({}) : createAgentsRunDatabaseClient({});
	try {
		console.log("Dropping workflow schema...");
		await dbClient.execute(sql`DROP SCHEMA IF EXISTS workflow CASCADE`);
		console.log("✅ Workflow schema dropped");
		console.log("Dropping public schema and all objects...");
		await dbClient.execute(sql`DROP SCHEMA IF EXISTS public CASCADE`);
		console.log("✅ Public schema dropped");
		console.log("Recreating public schema...");
		await dbClient.execute(sql`CREATE SCHEMA public`);
		console.log("✅ Public schema recreated");
		console.log("---");
		console.log("🎉 Database completely wiped - ready for fresh migrations");
	} catch (error) {
		console.error("❌ Failed to delete database:", error);
		throw error;
	}
}

//#endregion
export { deleteDatabase };