import type { ModerationAdapter, VerifiedPurchasePort } from '../types.js'

export interface FakeVerifiedPurchaseOptions {
  purchased?: boolean
  lookup?: (userId: string, productId: string, purchaseId: string) => boolean | Promise<boolean>
}

export function createFakeVerifiedPurchasePort(
  options: FakeVerifiedPurchaseOptions = {},
): VerifiedPurchasePort {
  const defaultPurchased = options.purchased ?? true

  return {
    async hasPurchased(userId, productId, purchaseId) {
      if (options.lookup) {
        return options.lookup(userId, productId, purchaseId)
      }
      return defaultPurchased
    },
  }
}

export interface FakeModerationOptions {
  action?: 'approve' | 'flag' | 'reject'
  score?: number
  scoreFn?: (text: string) => Promise<{ action: 'approve' | 'flag' | 'reject'; score: number }>
}

export function createFakeModerationAdapter(options: FakeModerationOptions = {}): ModerationAdapter {
  return {
    async score(text) {
      if (options.scoreFn) {
        return options.scoreFn(text)
      }
      return {
        action: options.action ?? 'flag',
        score: options.score ?? 0.5,
      }
    },
  }
}

/** D3 — trivially-wireable opt-in auto-publish when the host has no moderation infra. */
export const noOpAutoApproveModerationAdapter: ModerationAdapter = {
  async score() {
    return { action: 'approve', score: 1 }
  },
}

/** Reference adapter — always flags for manual queue (lands pending). */
export const manualQueueModerationAdapter: ModerationAdapter = {
  async score() {
    return { action: 'flag', score: 0.5 }
  },
}
