import * as Sentry from "@sentry/nextjs";
import type { ErrorReportingService } from "./error-reporting-service";
import type { NotificationService } from "../notifications/notification-service";

export interface GlitchtipConfig {
  enabled: boolean;
}

export class GlitchtipErrorReportingService implements ErrorReportingService {
  constructor(
    private readonly config: GlitchtipConfig,
    private readonly notifications?: NotificationService,
  ) {}

  captureException(error: unknown, context?: Record<string, unknown>) {
    if (!this.config.enabled) return;

    Sentry.withScope((scope) => {
      if (context) {
        scope.setExtras(context);
      }
      Sentry.captureException(error);
    });

    if (this.notifications) {
      this.notifications
        .notify({
          title: "🚨 Exception Captured",
          message: error instanceof Error ? error.message : String(error),
          priority: 10,
        })
        .catch((err) =>
          console.error("Failed to send notification for exception", err),
        );
    }
  }

  captureMessage(message: string, context?: Record<string, unknown>) {
    if (!this.config.enabled) return;

    Sentry.withScope((scope) => {
      if (context) {
        scope.setExtras(context);
      }
      Sentry.captureMessage(message);
    });
  }
}
