# retoor <retoor@molodetz.nl>

from __future__ import annotations

from .base import (
    MEDIUM,
    LOW,
    PageContext,
    SiteContext,
    ok_check,
    page_check,
)

CATEGORY = "social"

OG_REQUIRED = ("og:title", "og:description", "og:image", "og:url")
TWITTER_REQUIRED = ("twitter:card",)


@page_check
def open_graph(page: PageContext, site: SiteContext) -> list:
    og = page.dom.get("og", {}) or {}
    missing = [tag for tag in OG_REQUIRED if not og.get(tag)]
    results = [
        ok_check(
            "social.open_graph",
            CATEGORY,
            "Open Graph tags",
            not missing,
            severity=MEDIUM,
            value="complete" if not missing else f"missing {', '.join(missing)}",
            recommendation="Add og:title, og:description, og:image and og:url for rich social previews.",
            warn=True,
            details={"present": sorted(og.keys())},
            url=page.url,
        )
    ]
    return results


@page_check
def twitter_card(page: PageContext, site: SiteContext) -> list:
    twitter = page.dom.get("twitter", {}) or {}
    og = page.dom.get("og", {}) or {}
    has_card = bool(twitter.get("twitter:card"))
    return [
        ok_check(
            "social.twitter_card",
            CATEGORY,
            "Twitter Card",
            has_card or bool(og),
            severity=LOW,
            value=twitter.get("twitter:card", "") or ("falls back to OG" if og else "missing"),
            recommendation="Add a twitter:card meta tag (summary_large_image) for X/Twitter previews.",
            warn=True,
            details={"present": sorted(twitter.keys())},
            url=page.url,
        )
    ]
