// Node.js 22+. Set EMAILVERIFIER_API_KEY, then run: node verify-email.mjs person@example.com
const email = process.argv[2]
const apiKey = process.env.EMAILVERIFIER_API_KEY

try {
  if (!apiKey) throw new Error('Set EMAILVERIFIER_API_KEY to your project key.')
  if (process.argv.length !== 3 || !email || email.length > 254) throw new Error('Pass one email address: node verify-email.mjs person@example.com')
  const response = await fetch('https://emailverifier.dev/api/v1/verify', {
    method: 'POST',
    redirect: 'error',
    headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey },
    body: JSON.stringify({ email }),
    signal: AbortSignal.timeout(30_000)
  })
  if (!response.ok) throw new Error(`Verification request failed (HTTP ${response.status}). Check your key, credits, or rate limit before retrying.`)
  const result = await response.json()
  if (!['allow', 'review', 'block'].includes(result.action)) throw new Error('The API returned an unexpected response.')
  // Unknown results remain reviewable. Verification never replaces inbox ownership confirmation.
  console.log(JSON.stringify(result, null, 2))
} catch (error) {
  console.error(error instanceof Error ? error.message : 'Verification request failed.')
  process.exitCode = 1
}
