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

export const phoneRequiredEarnAdapter: FraudAdapter<EarnCtx> = {
  key: 'phone-required-earn',
  points: ['EARN'],
  async evaluate(ctx: EarnCtx, _cfg: AdapterConfig): Promise<FraudSignal | null> {
    // Load phones for both parties if not in ctx
    const [referrerPhone, refereePhone] = await Promise.all([
      ctx.host.getUserPhoneIndex(ctx.referrerUserId),
      ctx.host.getUserPhoneIndex(ctx.refereeUserId),
    ])

    if (!referrerPhone || !refereePhone) {
      return {
        adapter: 'phone-required-earn',
        action: 'block',
        codes: ['MISSING_VERIFIED_PHONE'],
        detail: { referrerHasPhone: !!referrerPhone, refereeHasPhone: !!refereePhone },
      }
    }

    // Same phone number on both accounts
    if (referrerPhone === refereePhone) {
      return {
        adapter: 'phone-required-earn',
        action: 'block',
        codes: ['SHARED_PHONE_NUMBER'],
        detail: {},
      }
    }

    return null
  },
}
