import { hashNamespace } from "@powerhousedao/shared/processors";
import { Kysely, PostgresDialect } from "kysely";
import { Pool } from "pg";
import { createRepository as createSubgraphRepository } from "../subgraphs/vetra-cloud-secrets/repository.js";
/**
 * Resolve the Postgres schema name for the given reactor namespace.
 *
 * Reactor-api hashes the human-readable namespace ("vetra-cloud-secrets")
 * to a fixed-length lowercase base-26 string and uses that as the schema
 * name. The standalone controller doesn't have access to the reactor-api
 * runtime — but it needs to read from the same schema — so we recompute
 * the hash directly using the shared package's `hashNamespace`.
 */
export function resolveSchema(namespace) {
    return hashNamespace(namespace);
}
/**
 * Build a `SecretsRepository` backed by a fresh Postgres pool the
 * controller fully owns (vs. the subgraph case which reuses the
 * reactor-api kysely). Returns the repo plus a `close()` for graceful
 * shutdown.
 */
export function createOwnedRepository(opts) {
    const schema = resolveSchema(opts.namespace);
    const pool = new Pool({ connectionString: opts.databaseUrl });
    const baseDb = new Kysely({
        dialect: new PostgresDialect({ pool }),
    });
    const db = baseDb.withSchema(schema);
    const repo = createSubgraphRepository(db);
    return {
        schema,
        envVarsForTenant: (tenantId) => repo.envVarsForTenant(tenantId),
        secretsForTenant: (tenantId) => repo.secretsForTenant(tenantId),
        allTenantIds: () => repo.allTenantIds(),
        close: () => baseDb.destroy(),
    };
}
