"""SubscribeStar — three quirks colocated.

1. external_post_id: gallery-dl puts the per-attachment id in `id`
   (e.g. 711509) and the actual post id in `post_id` (e.g. 360360).
   The default chain in base.py already prefers `post_id`; this module
   doesn't need to override it but the comment lives here too so a
   future reader knows the chain's order was driven by this platform.

2. post_url: gallery-dl's `url` is the file CDN URL
   (`/post_uploads?payload=...`). Synthesize the post permalink from
   `post_id`.

3. augment_cookies: the server gates artist pages behind a
   `_personalization_id` age-confirmation cookie that the user can't
   easily refresh — SubscribeStar's frontend JS uses localStorage to
   suppress the age popup once dismissed. gallery-dl's own login flow
   sidesteps this by setting `18_plus_agreement_generic=true`; we mirror
   that for cookies captured via the extension — on EVERY SubscribeStar
   domain, because cookies are domain-scoped and the wall exists on all
   of them: an .adult-only line never rides along to a .art creator page
   (Elasid, event #54116 — every poll 302'd to /age_confirmation_warning).
"""

from .base import GD_DEFAULTS, PlatformInfo, str_id_value


def derive_post_url(data: dict) -> str | None:
    pid = str_id_value(data.get("post_id"))
    if pid:
        return f"https://www.subscribestar.com/posts/{pid}"
    return None


# All domains SubscribeStar serves creators from; the age wall gates each.
_SS_DOMAINS = (".subscribestar.com", ".subscribestar.adult", ".subscribestar.art")


def augment_cookies(netscape: str) -> str:
    # Far-future expiry — gallery-dl's own login flow sets this with no
    # explicit expiry; the server only checks presence/value.
    expiry = 4102444800  # 2100-01-01 UTC
    body = netscape.rstrip("\n")
    if not body:
        body = "# Netscape HTTP Cookie File"
    lines = netscape.splitlines()
    for domain in _SS_DOMAINS:
        # Per-domain presence check: captured cookies carrying the age
        # cookie for one domain must not suppress injection on the others.
        if any(
            line.startswith(domain + "\t") and "18_plus_agreement_generic" in line
            for line in lines
        ):
            continue
        body += "\n" + "\t".join([
            domain, "TRUE", "/", "TRUE",
            str(expiry), "18_plus_agreement_generic", "true",
        ])
    return body + "\n"


INFO = PlatformInfo(
    key="subscribestar",
    name="SubscribeStar",
    description="Download posts from SubscribeStar creators",
    auth_type="cookies",
    requires_auth=True,
    url_pattern=r"^https?://(www\.)?subscribestar\.(com|adult|art)/",
    url_examples=[
        "https://subscribestar.adult/example_artist",
        "https://www.subscribestar.com/example_artist",
        "https://subscribestar.art/example_artist",
    ],
    default_config={**GD_DEFAULTS, "content_types": ["all"]},
    derive_post_url=derive_post_url,
    augment_cookies=augment_cookies,
)
