import * as Sentry from '@sentry/nestjs';

const isTest = process.env['NODE_ENV'] === 'test';

// Skip profiling integration in test env — the native binary may not be
// available for every Node.js version and it is unnecessary during E2E runs.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const integrations: any[] = [];
if (!isTest) {
  try {
    // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/consistent-type-imports
    const { nodeProfilingIntegration } = require('@sentry/profiling-node') as typeof import('@sentry/profiling-node');
    integrations.push(nodeProfilingIntegration());
  } catch {
    // Native CPU profiler binary not available — skip profiling gracefully.
    console.warn('[Sentry] Profiling skipped — native module not available');
  }
}

Sentry.init({
  dsn: process.env['SENTRY_DSN'],
  environment: process.env['NODE_ENV'] ?? 'development',
  integrations,
  tracesSampleRate: process.env['NODE_ENV'] === 'production' ? 0.2 : 1.0,
  profilesSampleRate: process.env['NODE_ENV'] === 'production' ? 0.2 : 1.0,
  enabled: !isTest && !!process.env['SENTRY_DSN'],
});
