"""id_utils.py — Python mirror of src/scripts/external-ids.mjs.

Pure helpers for normalizing external identifiers (DOI/arXiv/S2/URL).
No I/O, no side effects. Parity with the Node module is gated by
src/tools/tests/fixtures/id-cases.json.

Cross-reference: keep regex strings byte-for-byte aligned with
external-ids.mjs (`EXTERNAL_ID_PATTERNS`).
"""

from __future__ import annotations

import re
from types import MappingProxyType
from typing import Any, Mapping
from urllib.parse import urlsplit, urlunsplit, parse_qsl, urlencode, unquote

CANONICAL_URL_V = 1
EXTERNAL_ID_NAMESPACES = ("doi", "arxiv", "s2", "url", "openalex")
_NS_SET = frozenset(EXTERNAL_ID_NAMESPACES)
_MAX_URL_LEN = 2048
_MAX_VALUE_LEN = 2048

# Cross-ref: src/scripts/external-ids.mjs EXTERNAL_ID_PATTERNS
EXTERNAL_ID_PATTERNS: Mapping[str, "re.Pattern[str]"] = MappingProxyType({
    "doi":       re.compile(r"^10\.[0-9]{4,9}/[A-Za-z0-9._\-/()]{1,256}$"),
    "arxiv_new": re.compile(r"^[0-9]{4}\.[0-9]{4,5}(?:v[0-9]+)?$"),
    "arxiv_old": re.compile(r"^[a-z\-]+(?:\.[A-Z]{2})?/[0-9]{7}(?:v[0-9]+)?$"),
    "s2":        re.compile(r"^[a-f0-9]{40}$"),
    "doi_arxiv": re.compile(r"^10\.48550/arxiv\.([0-9]{4}\.[0-9]{4,5}(?:v[0-9]+)?)$", re.IGNORECASE),
    # OpenAlex Work ID only — reject Author (A), Institution (I), Publisher (P), Venue (V), Source (S).
    "openalex":  re.compile(r"^W[0-9]{1,12}$"),
})

_ARXIV_VERSION_RE = re.compile(r"v([0-9]+)$")
_TRACKING_PARAM_RE = re.compile(r"^(utm_|ref$|ref_)", re.IGNORECASE)
_DOI_URL_PREFIX_RE = re.compile(r"^https?://(?:dx\.)?doi\.org/", re.IGNORECASE)
_ARXIV_URL_PREFIX_RE = re.compile(r"^https?://arxiv\.org/(?:abs|pdf)/", re.IGNORECASE)
_DOI_PREFIX_RE = re.compile(r"^doi:", re.IGNORECASE)
_ARXIV_PREFIX_RE = re.compile(r"^arxiv:", re.IGNORECASE)
_PDF_SUFFIX_RE = re.compile(r"\.pdf$", re.IGNORECASE)
_DOI_URL_FULL_RE = re.compile(r"^https://doi\.org/(.+)$", re.IGNORECASE)
_ARXIV_URL_FULL_RE = re.compile(r"^https://arxiv\.org/(?:abs|pdf)/(.+?)(?:\.pdf)?$", re.IGNORECASE)
_OPENALEX_URL_PREFIX_RE = re.compile(r"^https?://(?:api\.)?openalex\.org/(?:works/)?", re.IGNORECASE)
_OPENALEX_PREFIX_RE = re.compile(r"^openalex:", re.IGNORECASE)
_OPENALEX_URL_FULL_RE = re.compile(r"^https://(?:api\.)?openalex\.org/(?:works/)?(W[0-9]{1,12})$", re.IGNORECASE)


def _decode_uri_safe(s: str) -> str:
    try:
        return unquote(s)
    except Exception:
        return s


def canonicalize_url(raw: str) -> str:
    """Lowercase host, force https, strip fragment + utm_*/ref* params."""
    if not isinstance(raw, str):
        raise TypeError("canonicalize_url: expected string")
    if len(raw) > _MAX_URL_LEN:
        raise ValueError(f"canonicalize_url: length > {_MAX_URL_LEN}")
    if not raw.isascii():
        raise ValueError("canonicalize_url: non-ASCII")
    parts = urlsplit(raw)
    if parts.scheme.lower() not in ("http", "https"):
        raise ValueError(f"canonicalize_url: unsupported protocol {parts.scheme}")
    if not parts.netloc:
        raise ValueError("canonicalize_url: missing netloc")
    scheme = "https"
    host = parts.hostname or ""
    host = host.lower()
    netloc = host
    if parts.port is not None and not (
        (scheme == "https" and parts.port == 443) or (scheme == "http" and parts.port == 80)
    ):
        netloc = f"{host}:{parts.port}"
    qs = parse_qsl(parts.query, keep_blank_values=True)
    qs = [(k, v) for (k, v) in qs if not _TRACKING_PARAM_RE.match(k)]
    qs.sort(key=lambda kv: kv[0])
    query = urlencode(qs)
    path = parts.path or "/"
    if len(path) > 1 and path.endswith("/"):
        path = path.rstrip("/")
    return urlunsplit((scheme, netloc, path, query, ""))


def normalize_external_id(kind: str, raw: Any) -> dict:
    """Normalize raw value for a namespace → dict with id/valid/extras."""
    extras: dict = {}
    if not isinstance(raw, str) or not raw or kind not in _NS_SET:
        return {"id": None, "valid": False, "extras": extras}
    trimmed = raw.strip()
    if not trimmed:
        return {"id": None, "valid": False, "extras": extras}

    if kind == "doi":
        body = _DOI_URL_PREFIX_RE.sub("", trimmed)
        body = _DOI_PREFIX_RE.sub("", body)
        body = _decode_uri_safe(body).lower()
        # See external-ids.mjs: explicitly reject '..' inside the body.
        if ".." in body:
            return {"id": None, "valid": False, "extras": extras}
        if not EXTERNAL_ID_PATTERNS["doi"].match(body):
            return {"id": None, "valid": False, "extras": extras}
        return {"id": body, "valid": True, "extras": extras}

    if kind == "arxiv":
        body = _ARXIV_URL_PREFIX_RE.sub("", trimmed)
        body = _ARXIV_PREFIX_RE.sub("", body)
        body = _PDF_SUFFIX_RE.sub("", body)
        m = _ARXIV_VERSION_RE.search(body)
        if m:
            extras["arxiv_version"] = int(m.group(1))
        base_id = _ARXIV_VERSION_RE.sub("", body)
        if not (EXTERNAL_ID_PATTERNS["arxiv_new"].match(base_id)
                or EXTERNAL_ID_PATTERNS["arxiv_old"].match(base_id)):
            return {"id": None, "valid": False, "extras": {}}
        return {"id": base_id, "valid": True, "extras": extras}

    if kind == "s2":
        body = trimmed.lower()
        if not EXTERNAL_ID_PATTERNS["s2"].match(body):
            return {"id": None, "valid": False, "extras": extras}
        return {"id": body, "valid": True, "extras": extras}

    if kind == "openalex":
        # Strip optional URL/`openalex:` prefixes. Work IDs are case-sensitive:
        # do NOT lowercase before regex check (leading `W` must remain capital).
        body = _OPENALEX_URL_PREFIX_RE.sub("", trimmed)
        body = _OPENALEX_PREFIX_RE.sub("", body)
        if not EXTERNAL_ID_PATTERNS["openalex"].match(body):
            return {"id": None, "valid": False, "extras": extras}
        return {"id": body, "valid": True, "extras": extras}

    if kind == "url":
        try:
            ident = canonicalize_url(trimmed)
        except Exception:
            return {"id": None, "valid": False, "extras": extras}
        return {"id": ident, "valid": True, "extras": {"canonical_v": CANONICAL_URL_V}}

    return {"id": None, "valid": False, "extras": extras}


def parse_url_to_external_ids(raw: Any) -> dict:
    """Inspect URL → namespaces it implies + canonical url."""
    out: dict = {}
    if not isinstance(raw, str) or len(raw) > _MAX_URL_LEN:
        return out
    try:
        canon = canonicalize_url(raw)
    except Exception:
        return out
    out["url"] = canon
    m = _DOI_URL_FULL_RE.match(canon)
    if m:
        r = normalize_external_id("doi", m.group(1))
        if r["valid"]:
            out["doi"] = r["id"]
    m = _ARXIV_URL_FULL_RE.match(canon)
    if m:
        r = normalize_external_id("arxiv", m.group(1))
        if r["valid"]:
            out["arxiv"] = r["id"]
    m = _OPENALEX_URL_FULL_RE.match(canon)
    if m:
        r = normalize_external_id("openalex", m.group(1))
        if r["valid"]:
            out["openalex"] = r["id"]
    return out


def expand_external_ids(ids: Any) -> dict:
    """Synthesize cross-namespace equivalents (arxiv↔arxiv-DOI). New dict."""
    out: dict = {}
    if not isinstance(ids, dict):
        return out
    for ns in EXTERNAL_ID_NAMESPACES:
        v = ids.get(ns)
        if isinstance(v, str) and v:
            out[ns] = v
    if out.get("arxiv") and not out.get("doi"):
        out["doi"] = f"10.48550/arxiv.{out['arxiv']}"
    elif out.get("doi") and not out.get("arxiv"):
        m = EXTERNAL_ID_PATTERNS["doi_arxiv"].match(out["doi"])
        if m:
            out["arxiv"] = m.group(1)
    return out


def external_id_match_key(ids: Any) -> str | None:
    """Best stable dedup key: doi > arxiv > s2 > url > openalex."""
    if not isinstance(ids, dict):
        return None
    for ns in EXTERNAL_ID_NAMESPACES:
        v = ids.get(ns)
        if isinstance(v, str) and v:
            return f"{ns}:{v}"
    return None


# ---------------------------------------------------------------------------
# Free-text identifier extractor — used by RSS/Atom fetcher to harvest DOIs
# and arXiv IDs from feed titles, summaries, and link hrefs.
# Reuses the compiled namespace regex so behavior stays aligned with
# normalize_external_id; no second parsing dialect.
# ---------------------------------------------------------------------------

_DOI_TEXT_RE = re.compile(r"\b10\.[0-9]{4,9}/[A-Za-z0-9._\-/()]{1,256}", re.IGNORECASE)
_ARXIV_TEXT_RE = re.compile(
    r"(?:arxiv\.org/(?:abs|pdf)/|arxiv:)([0-9]{4}\.[0-9]{4,5}(?:v[0-9]+)?)",
    re.IGNORECASE,
)
_OPENALEX_TEXT_RE = re.compile(r"openalex\.org/(?:works/)?(W[0-9]{1,12})", re.IGNORECASE)


def extract_ids_from_text(text: Any) -> dict:
    """Scan free text for DOI / arXiv / OpenAlex IDs.

    Returns a dict with keys `doi`, `arxiv`, `openalex` — each `str` or `None`.
    First valid match per namespace wins (later matches are ignored). The
    arxiv-DOI cross-walk is applied: an extracted arXiv ID synthesizes its
    `10.48550/arxiv.<id>` DOI form, and vice-versa.
    """
    out: dict = {"doi": None, "arxiv": None, "openalex": None}
    if not isinstance(text, str) or not text:
        return out

    # arXiv first because an arxiv-DOI like 10.48550/arxiv.X also matches the
    # generic DOI pattern; finding the arxiv form first lets us emit both.
    m = _ARXIV_TEXT_RE.search(text)
    if m:
        r = normalize_external_id("arxiv", m.group(1))
        if r["valid"]:
            out["arxiv"] = r["id"]

    m = _DOI_TEXT_RE.search(text)
    if m:
        r = normalize_external_id("doi", m.group(0))
        if r["valid"]:
            out["doi"] = r["id"]

    m = _OPENALEX_TEXT_RE.search(text)
    if m:
        r = normalize_external_id("openalex", m.group(1))
        if r["valid"]:
            out["openalex"] = r["id"]

    # Cross-walk after extraction so DOI inference doesn't suppress real DOI hits.
    if out["arxiv"] and not out["doi"]:
        out["doi"] = f"10.48550/arxiv.{out['arxiv']}"
    elif out["doi"] and not out["arxiv"]:
        m2 = EXTERNAL_ID_PATTERNS["doi_arxiv"].match(out["doi"])
        if m2:
            out["arxiv"] = m2.group(1)

    return out


_UNSAFE_TOKEN_RE = re.compile(r"[\x00-\x1f\\\"'`*?<>|]")


def safe_id_token(kind: str, val: Any) -> str:
    """Re-validate a value before path/glob concatenation."""
    if not isinstance(val, str) or not val:
        raise ValueError("safe_id_token: empty")
    if len(val) > _MAX_URL_LEN:
        raise ValueError("safe_id_token: too long")
    if _UNSAFE_TOKEN_RE.search(val):
        raise ValueError("safe_id_token: control or meta char")
    if ".." in val:
        raise ValueError("safe_id_token: traversal")
    r = normalize_external_id(kind, val)
    if not r["valid"] or r["id"] != val:
        raise ValueError(f"safe_id_token: not a valid {kind}")
    return val


def build_external_ids(candidates: Any) -> dict:
    """Build a validated external_ids dict from a flat `{ns: raw}` candidate map.

    Each candidate is run through `normalize_external_id`; invalid values are
    dropped silently. Use this in fetchers that already know their per-field
    mapping (DOI, ArXiv, paperId, url) — the validation choke point makes
    every fetcher safe by construction.
    """
    out: dict = {}
    if not isinstance(candidates, dict):
        return out
    for ns in EXTERNAL_ID_NAMESPACES:
        raw = candidates.get(ns)
        if not isinstance(raw, str) or not raw:
            continue
        r = normalize_external_id(ns, raw)
        if r["valid"] and r["id"]:
            out[ns] = r["id"]
    return out


_PROVIDER_SLUG_RE = re.compile(r"^[a-z][a-z0-9_-]{0,31}$")


def build_source_entry(
    provider: str,
    url: str | None = None,
    fetched_at: str | None = None,
    ns: str | None = None,
    value: str | None = None,
) -> dict:
    """Build one provenance entry for the `sources` frontmatter array.

    Mirror of `buildSourceEntry` in external-ids.mjs. Pure helper; caller is
    responsible for appending into the existing array.

    `ns` + `value` (added 2026-05) record *which* external identifier this
    provider resolved. Both must be present together to persist; if either is
    missing or invalid, both are dropped silently — same forgiveness model as
    the existing `url` field.
    """
    if not isinstance(provider, str) or not _PROVIDER_SLUG_RE.match(provider):
        raise ValueError(f"build_source_entry: invalid provider: {provider!r}")
    if not isinstance(fetched_at, str) or not fetched_at:
        from datetime import datetime, timezone
        fetched_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
    entry: dict = {"provider": provider, "fetched_at": fetched_at}
    if isinstance(url, str) and url and len(url) <= _MAX_URL_LEN:
        # Best-effort URL parse — drop on failure so junk like "not a url" is
        # never persisted. Canonicalization NOT applied: provenance records
        # the URL the user/skill saw, not a normalized form.
        try:
            parts = urlsplit(url)
            if parts.scheme and parts.netloc:
                entry["url"] = url
        except Exception:
            pass
    if (
        isinstance(ns, str) and ns in _NS_SET
        and isinstance(value, str) and value
        and len(value) <= _MAX_VALUE_LEN
    ):
        entry["ns"] = ns
        entry["value"] = value
    return entry


def sanitize_external_ids_object(obj: Any) -> dict:
    """Allowlist filter; rejects __proto__/constructor/etc."""
    out: dict = {}
    if not isinstance(obj, dict):
        return out
    for ns in EXTERNAL_ID_NAMESPACES:
        if ns not in obj:
            continue
        v = obj[ns]
        if not isinstance(v, str) or not v:
            continue
        out[ns] = v
    return out
