// Sentry integration — server-side error reporter.
//
// Activation: set SENTRY_DSN in the deployment environment. When SENTRY_DSN
// is unset, the helpers below are no-ops (the app falls back to the
// existing structured logger only).
//
// Why a thin wrapper instead of full Sentry SDK init: keeps this branch
// merge-safe before the operator adds the DSN. Once the DSN is set, swap
// the no-op implementation here for `@sentry/nextjs` init code; the
// module's public shape stays identical so call-sites don't change.
//
// Source finding: mo2 P0-5 (no error monitoring), Open_Findings.md § 3.

const SENTRY_DSN = process.env.SENTRY_DSN

export function isSentryEnabled(): boolean {
  return Boolean(SENTRY_DSN && SENTRY_DSN.length > 16)
}

// no-op stand-in until @sentry/nextjs is wired up.
// Operator activation steps:
//   1. `npm install @sentry/nextjs` (defer until we know we need it).
//   2. Replace this file's body with `import * as Sentry from '@sentry/nextjs'`
//      and `Sentry.init({ dsn: SENTRY_DSN, environment: process.env.NEXT_PUBLIC_APP_ENV, ... })`.
//   3. Re-export `Sentry.captureException` / `Sentry.captureMessage` directly.
//   4. Run `npx @sentry/wizard@latest -i nextjs` ONCE; it scaffolds the
//      sentry.client/server/edge.config.ts files Vercel expects.

export function captureException(err: unknown, context?: Record<string, unknown>): void {
  if (!isSentryEnabled()) return
  // Lazy hook for future Sentry import. Until then we forward to console
  // in dev only; prod errors already flow through lib/logger.
  if (process.env.NODE_ENV !== 'production') {
    // eslint-disable-next-line no-console
    console.error('[sentry-stub]', err, context)
  }
}

export function captureMessage(message: string, context?: Record<string, unknown>): void {
  if (!isSentryEnabled()) return
  if (process.env.NODE_ENV !== 'production') {
    // eslint-disable-next-line no-console
    console.warn('[sentry-stub]', message, context)
  }
}
