import { defineNitroConfig } from "nitropack/config";

export default defineNitroConfig({
  compatibilityDate: "latest",
  srcDir: "server",
  preset: "node-server",

  // Supabase als external dep — nicht bundlen
  externals: {
    inline: [/^(?!@supabase\/supabase-js)/],
  },

  imports: {
    dirs: ["db", "db/**", "utils", "utils/**"],
    exclude: ["**/node_modules/**"],
  },

  runtimeConfig: {
    // ─── Database / Core ─────────────────────────────────────────────────
    databaseUrl: process.env.DATABASE_URL ?? process.env.NUXT_DATABASE_URL ?? "",
    encryptionKey: process.env.ENCRYPTION_KEY ?? "",

    // ─── Admin / Cron ────────────────────────────────────────────────────
    adminSecret: process.env.ADMIN_SECRET ?? "",
    cronSecret: process.env.CRON_SECRET ?? "",

    // ─── LLM-Provider ────────────────────────────────────────────────────
    // Infisical staging hat NUXT_*-prefix für openrouter+groq, andere ohne.
    openrouterApiKey: process.env.OPENROUTER_API_KEY ?? process.env.NUXT_OPENROUTER_API_KEY ?? "",
    openaiApiKey: process.env.OPENAI_API_KEY ?? process.env.NUXT_OPENAI_API_KEY ?? "",
    groqApiKey: process.env.GROQ_API_KEY ?? process.env.NUXT_GROQ_API_KEY ?? "",
    googleAiApiKey: process.env.GOOGLE_AI_API_KEY ?? "",

    // ─── TTS-Provider ────────────────────────────────────────────────────
    googleApiKey: process.env.GOOGLE_API_KEY ?? process.env.NUXT_GOOGLE_API_KEY ?? "",
    deepgramApiKey: process.env.DEEPGRAM_API_KEY ?? process.env.NUXT_DEEPGRAM_API_KEY ?? "",
    azureTtsKey: process.env.AZURE_TTS_KEY ?? "",
    azureTtsRegion: process.env.AZURE_TTS_REGION ?? "",
    // NEU im backend/-Layout (existieren in nuxt.config.ts NICHT, aber backend code liest sie)
    cartesiaApiKey: process.env.CARTESIA_API_KEY ?? "",
    cartesiaVoiceId: process.env.CARTESIA_VOICE_ID ?? "",
    elevenlabsApiKey: process.env.ELEVENLABS_API_KEY ?? "",
    elevenlabsVoiceId: process.env.ELEVENLABS_VOICE_ID ?? "",

    // ─── Supabase (Server-only) ──────────────────────────────────────────
    // Im alten Nuxt-Layout via @nuxtjs/supabase auto-injected. In standalone Nitro
    // explizit deklarieren, damit auth/middleware nicht 500't.
    // server/utils/auth.ts:32 liest `config.public.supabase ?? config.supabase`,
    // also beide Pfade müssen existieren.
    // Infisical staging-Namen: SUPABASE_KEY (nicht ANON_KEY), SUPABASE_SERVICE_KEY
    // (nicht SERVICE_ROLE_KEY). NIE umbenennen ohne Infisical-secret-rotation.
    supabaseUrl: process.env.SUPABASE_URL ?? "https://db-staging.rebreak.org",
    supabaseAnonKey: process.env.SUPABASE_KEY ?? process.env.SUPABASE_ANON_KEY ?? "",
    supabaseServiceKey: process.env.SUPABASE_SERVICE_KEY ?? process.env.SUPABASE_SERVICE_ROLE_KEY ?? "",

    // ─── Stripe ──────────────────────────────────────────────────────────
    stripeSecretKey: process.env.STRIPE_SECRET_KEY ?? "",
    stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET ?? "",

    // ─── Email / External APIs ───────────────────────────────────────────
    resendApiKey: process.env.RESEND_API_KEY ?? "",

    // ─── Bot-User-IDs (DB-User-References für Lyra/Rebreak-Bot-Posts) ────
    lyraBotUserId: process.env.LYRA_BOT_USER_ID ?? "",
    rebreakBotUserId: process.env.REBREAK_BOT_USER_ID ?? "",

    // ─── Public (client-readable) ────────────────────────────────────────
    public: {
      stripePublishableKey: process.env.STRIPE_PUBLISHABLE_KEY ?? "",
      appUrl: process.env.APP_URL ?? "https://staging.rebreak.org",
      apiBase: process.env.API_BASE ?? "https://staging.rebreak.org",
      // server/utils/auth.ts liest config.public.supabase.{url,key}
      // — wenn das fehlt, 500-cascade auf allen authentifizierten Routes
      // (Incident 2026-05-06).
      supabase: {
        url:
          process.env.SUPABASE_URL ??
          "https://db-staging.rebreak.org",
        key: process.env.SUPABASE_KEY ?? process.env.SUPABASE_ANON_KEY ?? "",
      },
    },
  },
});
