# retoor <retoor@molodetz.nl>

from __future__ import annotations

from .base import (
    HIGH,
    MEDIUM,
    LOW,
    PASS,
    WARN,
    Check,
    PageContext,
    SiteContext,
    ok_check,
    page_check,
)

CATEGORY = "meta"

TITLE_MIN = 30
TITLE_MAX = 60
DESC_MIN = 70
DESC_MAX = 160


@page_check
def title(page: PageContext, site: SiteContext) -> list:
    text = (page.dom.get("title", "") or "").strip()
    count = int(page.dom.get("titleCount", 0) or 0)
    results = [
        ok_check(
            "meta.title_present",
            CATEGORY,
            "Title tag",
            bool(text),
            severity=HIGH,
            value=text or "missing",
            recommendation="Add a unique, descriptive <title>.",
            url=page.url,
        )
    ]
    if text:
        length = len(text)
        good = TITLE_MIN <= length <= TITLE_MAX
        results.append(
            ok_check(
                "meta.title_length",
                CATEGORY,
                "Title length",
                good,
                severity=LOW,
                value=f"{length} chars",
                recommendation=f"Keep titles between {TITLE_MIN} and {TITLE_MAX} characters.",
                warn=True,
                url=page.url,
            )
        )
    if count > 1:
        results.append(
            Check(
                "meta.title_single",
                CATEGORY,
                "Single title tag",
                WARN,
                LOW,
                value=f"{count} titles",
                recommendation="Use exactly one <title> element.",
                url=page.url,
            )
        )
    return results


@page_check
def description(page: PageContext, site: SiteContext) -> list:
    text = (page.dom.get("metaDescription", "") or "").strip()
    results = [
        ok_check(
            "meta.description_present",
            CATEGORY,
            "Meta description",
            bool(text),
            severity=MEDIUM,
            value=text[:120] or "missing",
            recommendation="Write a compelling 70-160 character meta description.",
            warn=True,
            url=page.url,
        )
    ]
    if text:
        length = len(text)
        good = DESC_MIN <= length <= DESC_MAX
        results.append(
            ok_check(
                "meta.description_length",
                CATEGORY,
                "Description length",
                good,
                severity=LOW,
                value=f"{length} chars",
                recommendation=f"Keep meta descriptions between {DESC_MIN} and {DESC_MAX} characters.",
                warn=True,
                url=page.url,
            )
        )
    return results


@page_check
def lang(page: PageContext, site: SiteContext) -> Check:
    value = (page.dom.get("htmlLang", "") or "").strip()
    return ok_check(
        "meta.html_lang",
        CATEGORY,
        "Document language",
        bool(value),
        severity=LOW,
        value=value or "missing",
        recommendation="Declare the page language with <html lang=...>.",
        warn=True,
        url=page.url,
    )


@page_check
def charset(page: PageContext, site: SiteContext) -> Check:
    value = (page.dom.get("charset", "") or "").strip()
    return ok_check(
        "meta.charset",
        CATEGORY,
        "Character encoding",
        bool(value),
        severity=LOW,
        value=value or "missing",
        recommendation="Declare a <meta charset> (UTF-8 recommended).",
        warn=True,
        url=page.url,
    )


@page_check
def viewport(page: PageContext, site: SiteContext) -> Check:
    present = bool(page.dom.get("hasViewport"))
    return ok_check(
        "meta.viewport",
        CATEGORY,
        "Mobile viewport",
        present,
        severity=HIGH,
        value=page.dom.get("viewportContent", "") or ("present" if present else "missing"),
        recommendation="Add <meta name=viewport content='width=device-width, initial-scale=1'>.",
        url=page.url,
    )


@page_check
def favicon(page: PageContext, site: SiteContext) -> Check:
    present = bool(page.dom.get("favicon"))
    return ok_check(
        "meta.favicon",
        CATEGORY,
        "Favicon",
        present,
        severity=LOW,
        value="present" if present else "missing",
        recommendation="Provide a favicon for brand recognition in results and tabs.",
        warn=True,
        url=page.url,
    )
