import * as Sentry from "@sentry/node"

import { AuthStrategy, AuthResult } from "./auth-strategy.js"
import { userForAPIKeyAUTH } from "./auth0-cf.js"

export class JwtAuth0Strategy extends AuthStrategy {
  constructor(apiKey: string) {
    super(apiKey)
  }

  async authenticate(): Promise<AuthResult | null> {
    console.log(`Authenticating internal API key via Auth0`)
    try {
      const userId = await userForAPIKeyAUTH(this.apiKey)
      if (!userId) {
        return null
      }
      // JWT auth doesn't have organization context
      return {
        userId,
        organizationId: null,
      }
    } catch (err) {
      console.error("[Auth] Auth0 token validation failed:", err)
      Sentry.captureException(err)
      return null
    }
  }
}
