import { getApiKeyInfo } from "@codeflash-ai/common"
import { AuthStrategy, AuthResult } from "./auth-strategy.js"

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

  async authenticate(): Promise<AuthResult | null> {
    console.log(`[Auth] Authenticating using API key`)
    const apiKeyInfo = await getApiKeyInfo(this.apiKey)
    if (!apiKeyInfo) {
      console.log(`[Auth] API key not found`)
      return null
    }
    console.log(`[Auth] Authentication result userId: ${apiKeyInfo.user_id}, orgId: ${apiKeyInfo.organization_id}`)
    return {
      userId: apiKeyInfo.user_id,
      organizationId: apiKeyInfo.organization_id,
    }
  }
}
