import { manage_schema_exports } from "./manage-schema.js";
import { env, loadEnvironmentFiles } from "../../env.js";
import { getLogger } from "../../utils/logger.js";
import { tryAttachDatabasePool } from "../../utils/database-pool.js";
import { getDatabaseErrorLogContext } from "../../utils/error.js";
import { createTestManageDatabaseClientNoMigrations } from "./test-manage-client.js";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

//#region src/db/manage/manage-client.ts
loadEnvironmentFiles();
const managePoolLogger = getLogger("manage-db-pool");
function createAgentsManageDatabasePool(config) {
	const connectionString = config.connectionString || env.INKEEP_AGENTS_MANAGE_DATABASE_URL;
	if (!connectionString) throw new Error("INKEEP_AGENTS_MANAGE_DATABASE_URL environment variable is required. Please set it to your PostgreSQL connection string.");
	const pool = new Pool({
		connectionString,
		max: config.poolSize || Number(env.POSTGRES_POOL_SIZE) || 100,
		keepAlive: true,
		keepAliveInitialDelayMillis: 6e4,
		connectionTimeoutMillis: 1e4,
		idleTimeoutMillis: 3e4
	});
	tryAttachDatabasePool(pool);
	pool.on("error", (err) => {
		managePoolLogger.error({
			error: err,
			...getDatabaseErrorLogContext(err)
		}, "Unexpected error on agents manage database pool (Doltgres)");
	});
	return pool;
}
/**
* Creates a PostgreSQL database client with connection pooling
*/
function createAgentsManageDatabaseClient(config) {
	const connectionString = config.connectionString || env.INKEEP_AGENTS_MANAGE_DATABASE_URL;
	if (env.ENVIRONMENT === "test") return createTestManageDatabaseClientNoMigrations();
	if (!connectionString) throw new Error("INKEEP_AGENTS_MANAGE_DATABASE_URL environment variable is required. Please set it to your PostgreSQL connection string.");
	const pool = new Pool({
		connectionString,
		max: config.poolSize || Number(env.POSTGRES_POOL_SIZE) || 100,
		keepAlive: true,
		keepAliveInitialDelayMillis: 6e4,
		connectionTimeoutMillis: 1e4,
		idleTimeoutMillis: 3e4
	});
	tryAttachDatabasePool(pool);
	pool.on("error", (err) => {
		managePoolLogger.error({
			error: err,
			...getDatabaseErrorLogContext(err)
		}, "Unexpected error on agents manage database pool (Doltgres)");
	});
	return drizzle(pool, {
		schema: manage_schema_exports,
		logger: config.logger
	});
}
function createAgentManageDatabaseConnection(config) {
	const connectionString = config.connectionString || env.INKEEP_AGENTS_MANAGE_DATABASE_URL;
	if (env.ENVIRONMENT === "test") throw new Error("createAgentManageDatabaseConnection is not supported in test environment");
	if (!connectionString) throw new Error("INKEEP_AGENTS_MANAGE_DATABASE_URL environment variable is required. Please set it to your PostgreSQL connection string.");
	const pool = new Pool({
		connectionString,
		max: 1,
		keepAlive: true,
		keepAliveInitialDelayMillis: 6e4,
		connectionTimeoutMillis: 1e4
	});
	pool.on("error", (err) => {
		managePoolLogger.error({
			error: err,
			...getDatabaseErrorLogContext(err)
		}, "Unexpected error on agents manage database pool (Doltgres)");
	});
	return pool.connect().then((connection) => {
		const db = drizzle(connection, {
			schema: manage_schema_exports,
			logger: config.logger
		});
		const release = async () => {
			connection.release();
			await pool.end();
		};
		return {
			db,
			release
		};
	});
}

//#endregion
export { createAgentManageDatabaseConnection, createAgentsManageDatabaseClient, createAgentsManageDatabasePool };