import type { FraudAdapter, SignupCtx, FraudSignal, AdapterConfig } from '../types.js'

/**
 * CF bot score from request context.
 * cfBotScore: 0..99 (Cloudflare), lower = more likely bot.
 * Default threshold: score <= 30 → flag.
 */
export const botSignupAdapter: FraudAdapter<SignupCtx> = {
  key: 'bot-signup',
  points: ['SIGNUP'],
  async evaluate(ctx: SignupCtx, cfg: AdapterConfig): Promise<FraudSignal | null> {
    if (ctx.cfBotScore === undefined || ctx.cfBotScore === null) return null
    const threshold = (cfg.params?.['threshold'] as number | undefined) ?? 30
    if (ctx.cfBotScore > threshold) return null

    return {
      adapter: 'bot-signup',
      action: 'flag',
      codes: ['LOW_BOT_SCORE'],
      detail: { botScore: ctx.cfBotScore, threshold },
    }
  },
}
