"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listNamespaces = listNamespaces;
exports.getDefaultNamespace = getDefaultNamespace;
exports.getNamespace = getNamespace;
exports.registerNamespace = registerNamespace;
const db_1 = require("../../lib/db");
const sql_1 = require("./sql");
/**
 * List all registered namespaces, ordered by default-first then name.
 */
async function listNamespaces() {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.LIST_NAMESPACES);
    return rows;
}
/**
 * Get the default namespace.
 */
async function getDefaultNamespace() {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_DEFAULT_NAMESPACE);
    return rows[0] ?? null;
}
/**
 * Get a namespace by name.
 */
async function getNamespace(name) {
    const pool = (0, db_1.getPool)();
    const { rows } = await pool.query(sql_1.GET_NAMESPACE_BY_NAME, [name]);
    return rows[0] ?? null;
}
/**
 * Register (upsert) a namespace. Used by YAML deployer to auto-register.
 */
async function registerNamespace(name, description, metadata) {
    const pool = (0, db_1.getPool)();
    const schemaName = name; // HotMesh appId === Postgres schema name
    const { rows } = await pool.query(sql_1.UPSERT_NAMESPACE, [name, schemaName, description ?? null, metadata ? JSON.stringify(metadata) : null]);
    return rows[0];
}
