"""Inbound bot detection for the /jump affiliate redirect.

Declared crawlers (Applebot, Meta's meta-externalagent, Googlebot, ClaudeBot,
Amazonbot, other AI/SEO crawlers, scripts/scrapers) are forwarded to the raw
retailer URL by the /jump route WITHOUT an affiliate wrap and WITHOUT a
jump_log tally. Rationale (log analysis 2026-06-05, ~232k /jump hits / 90 days):

  - >= 33% of /jump requests are self-identified bots by User-Agent; Applebot +
    Meta's crawler alone are the #1 and #2 user-agents = ~25% of all clicks.
  - They have zero purchase intent and largely ignore robots.txt, so routing
    them through AvantLink is invalid affiliate traffic that can hurt our
    standing, and counting them pollutes the click metrics the dashboard shows.

DELIBERATELY CONSERVATIVE on browser-like UA strings: a false positive forwards
a real human to the unwrapped URL and silently loses a commission. So we match
ONLY tokens that never appear in a genuine desktop/mobile browser UA. Stealth
scrapers wearing a real browser UA are NOT caught here (cannot be, without
risking humans); that is a separate behavioral problem, not this filter's job.
"""
import re

# Case-insensitive. Every token below is absent from real Chrome/Safari/Firefox/
# Edge UA strings (verified against the top browser UAs in our own logs).
_BOT_RE = re.compile(
    r"bot|crawl|spider|slurp|"
    r"externalagent|externalhit|"                 # meta-externalagent, facebookexternalhit
    r"googleother|google-extended|google-inspectiontool|storebot|"  # Google crawlers w/o "bot"
    r"claude|gptbot|chatgpt|openai|anthropic|perplexity|"           # AI crawlers
    r"bytespider|petalbot|dataforseo|ahrefs|semrush|mj12|dotbot|"   # SEO crawlers
    r"yandex|baidu|sogou|seznam|"
    r"archive\.org|ia_archiver|"
    r"embedly|skypeuripreview|pinterest|slackbot|telegrambot|whatsapp|discordbot|"  # link unfurlers
    r"python-|python/|curl/|wget|libwww|java/|go-http|okhttp|"      # scripts / HTTP libs
    r"scrapy|httpx|aiohttp|node-fetch|axios|guzzle|requests/|"
    r"headless|phantomjs|puppeteer|playwright|selenium|"           # automation
    r"zgrab|masscan|censys|nmap|nikto|"                            # scanners
    r"pingdom|uptimerobot|statuscake|datadog|newrelic",            # monitors
    re.IGNORECASE,
)


def is_bot(ua: str) -> bool:
    """True if the User-Agent looks like a crawler/agent/tool that should be
    forwarded to the bare URL (no affiliate wrap, no click tally).

    An empty/missing UA is treated as a bot: no real browser omits it, and the
    cost of being wrong is only an unwrapped redirect, never a broken one.
    """
    ua = (ua or "").strip()
    if not ua:
        return True
    return _BOT_RE.search(ua) is not None
