import { generateSubdomain } from "../../shared/subdomain-generator.js";
import { getTenantId } from "../../processors/vetra-cloud-environment/gitops.js";
import { setLabel, initialize, addPackage, enableService, approveChanges, } from "../../document-models/vetra-cloud-environment/v1/gen/creators.js";
/**
 * Create one ownerless, key-less studio environment and drive it to
 * CHANGES_APPROVED so the existing processor deploys it. No SET_OWNER here — the
 * env stays owner-null until claimed (claim sets the owner via system action).
 *
 * Actions are system-signed (no user in context), so `assertOwner` passes on the
 * owner-null doc without auto-claiming. Order mirrors the frontend cold flow.
 */
export async function createStudioEnvironmentDoc(reactor, cfg) {
    const documentId = await reactor.createDocument();
    const subdomain = generateSubdomain(documentId);
    const tenantId = getTenantId(subdomain, documentId);
    await reactor.execute(documentId, "main", [
        setLabel({ label: "Vetra Studio" }),
        initialize({
            genericSubdomain: subdomain,
            genericBaseDomain: "vetra.io",
            defaultPackageRegistry: cfg.registry,
        }),
        addPackage({ packageName: "vetra-cli", version: cfg.version }),
        enableService({
            type: "CLINT",
            prefix: "vetra-agent",
            clintConfig: {
                package: { registry: cfg.registry, name: "vetra-cli", version: cfg.version },
                env: [
                    { name: "VETRA_OBSERVABILITY_CONSENT", value: "granted", isSecret: false },
                    // Gate agent work on a credential: an unclaimed (key-less) warm pod
                    // refuses to run until a claim injects the key — so "no key" is the
                    // lock, complementing/replacing the network policy.
                    { name: "VETRA_REQUIRE_API_KEY", value: "true", isSecret: false },
                ],
                serviceCommand: "vetra",
                selectedRessource: cfg.sizeName,
            },
            selectedRessource: cfg.sizeName,
        }),
        approveChanges({}),
    ]);
    return { documentId, subdomain, tenantId };
}
