import { config } from "dotenv";
config();

import { PrismaClient } from "@prisma/client";
import { bootstrapAlphaSilo } from "../src/deployment/bootstrap-silo.js";
import { loadLocalSecretKeyring, LocalSecretEnvelope } from "../src/security/secretEnvelope.js";

async function main(): Promise<void> {
  const keyringFile = process.env["HUB_SECRET_KEYRING_FILE"];
  if (!keyringFile) throw new Error("HUB_SECRET_KEYRING_FILE is required");
  const secrets = new LocalSecretEnvelope(await loadLocalSecretKeyring());
  const prisma = new PrismaClient();
  try {
    const result = await bootstrapAlphaSilo(
      prisma,
      secrets,
      {
        organization: {
          id: "org_default",
          slug: "default",
          name: "Default Organization",
        },
        owner: {
          openId: process.env["FEISHU_BOT_OPEN_ID"] ?? "dev-owner",
          displayName: "Dev Owner",
        },
        feishu: {
          appId: process.env["FEISHU_APP_ID"] ?? "dev-app",
          appSecret: process.env["FEISHU_APP_SECRET"] ?? "dev-secret",
          botOpenId: process.env["FEISHU_BOT_OPEN_ID"] ?? "dev-bot",
        },
        provider: {
          providerId: "openrouter",
          baseUrl: process.env["ANTHROPIC_BASE_URL"] ?? "https://openrouter.ai/api",
          authToken: process.env["ANTHROPIC_AUTH_TOKEN"] ?? "dev-token",
        },
      },
      {
        feishu: async () => undefined,
        provider: async () => undefined,
      },
    );
    console.log("bootstrap ok:", JSON.stringify(result, null, 2));
  } finally {
    await prisma.$disconnect();
  }
}

main().catch((error: unknown) => {
  console.error("[dev-bootstrap] failed:", error instanceof Error ? error.message : String(error));
  process.exitCode = 1;
});
