"""Duplicate detection helpers.

Primary identity is the normalised LinkedIn profile URL. When a row has no URL we
fall back to a normalised ``fullName|company`` key.
"""

from __future__ import annotations

import re
from typing import Any
from urllib.parse import urlsplit

_WHITESPACE_RE = re.compile(r"\s+")
_NON_ALNUM_RE = re.compile(r"[^a-z0-9 ]+")


def normalize_profile_url(url: str | None) -> str | None:
    """Reduce a LinkedIn profile URL to a stable comparable form.

    ``https://www.linkedin.com/in/John-Smith-123/?utm=x`` -> ``linkedin.com/in/john-smith-123``
    """
    if not url:
        return None
    raw = url.strip()
    if not raw:
        return None
    if "://" not in raw:
        raw = "https://" + raw.lstrip("/")

    parts = urlsplit(raw)
    host = (parts.netloc or "").lower()
    host = host.removeprefix("www.")
    for prefix in ("in.", "uk.", "de.", "fr.", "br.", "ca.", "au."):
        if host.startswith(prefix) and host.endswith("linkedin.com"):
            host = "linkedin.com"
    path = (parts.path or "").rstrip("/").lower()
    path = path.replace("/mwlite", "")
    if not path:
        return host or None
    return f"{host}{path}"


def normalize_text(value: str | None) -> str:
    if not value:
        return ""
    lowered = value.strip().lower()
    lowered = _NON_ALNUM_RE.sub(" ", lowered)
    return _WHITESPACE_RE.sub(" ", lowered).strip()


def build_dedupe_key(
    profile_url: str | None = None,
    full_name: str | None = None,
    company: str | None = None,
) -> str | None:
    """The value stored on ``Lead.dedupe_key`` and used to detect repeats."""
    normalized_url = normalize_profile_url(profile_url)
    if normalized_url:
        return f"url:{normalized_url}"
    name = normalize_text(full_name)
    if not name:
        return None
    return f"name:{name}|{normalize_text(company)}"


def dedupe_key_for(data: dict[str, Any]) -> str | None:
    return build_dedupe_key(
        data.get("profile_url") or data.get("profileUrl"),
        data.get("full_name") or data.get("fullName"),
        data.get("company"),
    )
