"""Sister-site network for cross-promotion (Tier 0 cross-promo).

The canonical source of truth for the family is `sites/registry.yml` at the
repo root. That file is NOT shipped in the deploy tarball (deploy packages only
the `web/` tree), so this module carries a small baked copy of the network
metadata used at render time. Keep it in sync with registry.yml.

Production follow-up (see TODO "cross-promotion of sister sites"): GENERATE this
list from registry.yml at deploy time so it stays zero-touch. For the Tier-0
prototype a hand-kept list is fine.

Each entry: key, name (display wordmark), accent (the emerald-accented substring
of name), blurb (one line), url (apex), status ('live' | 'coming_soon').
"""

# Which site is rendering (this is the arpartsfinder web tree).
CURRENT_SITE = "arpartsfinder"

_NETWORK = [
    {"key": "arpartsfinder", "name": "ARPartsFinder", "accent": "AR",
     "blurb": "In-stock AR-15 & AR-10 parts", "url": "https://arpartsfinder.com",
     "status": "live"},
    {"key": "onlineammofinder", "name": "OnlineAmmoFinder", "accent": "Ammo",
     "blurb": "Compare ammo by cost-per-round", "url": "https://onlineammofinder.com",
     "status": "live"},
    # {"key": "handgunfinder", "name": "HandgunFinder", "accent": "Handgun",
    #  "blurb": "In-stock handgun parts & deals", "url": "https://handgunfinder.com",
    #  "status": "coming_soon"},
]


def _utm(url, medium):
    """Tag a cross-link so referral views are attributable in analytics."""
    sep = "&" if "?" in url else "?"
    return (f"{url}{sep}utm_source={CURRENT_SITE}"
            f"&utm_medium={medium}&utm_campaign=network")


def sisters(medium="footer"):
    """The other sites in the family, with wordmark split + UTM href."""
    out = []
    for s in _NETWORK:
        if s["key"] == CURRENT_SITE:
            continue
        name, acc = s["name"], s.get("accent") or ""
        i = name.find(acc) if acc else -1
        pre, post = (name[:i], name[i + len(acc):]) if i >= 0 else (name, "")
        if i < 0:
            acc = ""
        out.append({**s, "pre": pre, "accent": acc, "post": post,
                    "href": _utm(s["url"], medium)})
    return out
