// tdd-guard:skip — Next.js framework-loaded startup hook, not unit-testable
// in isolation. Validation logic lives in lib/env-schema.ts which has its own
// test file.
import * as Sentry from "@sentry/nextjs"
import { registerOTel } from "@vercel/otel"

export async function register() {
  // Force env validation at server start (KAR-522 Source 15 + 12-Factor III).
  // Importing the schema runs createEnv() once, which throws on missing or
  // invalid required vars before any request is served.
  await import("./lib/env-schema")

  // OpenTelemetry auto-instrumentation (KAR-522 Source 8, KAR-532).
  // Runs alongside Sentry — Sentry-OTel-integration uses the same
  // TraceProvider when @vercel/otel is wired here. Trace context (W3C
  // traceparent) propagates from incoming request → Next.js → fetch.
  registerOTel({
    serviceName: process.env.LOG_SERVICE ?? "kadi-backend",
  })

  if (process.env.NEXT_RUNTIME === "nodejs") {
    await import("./sentry.server.config")
  }

  if (process.env.NEXT_RUNTIME === "edge") {
    await import("./sentry.edge.config")
  }
}

export const onRequestError = Sentry.captureRequestError
