"""
Standalone Jinja2 email template renderer.

Used by both the worker (outside Quart request context) and admin gallery routes.
Creates a module-level Environment pointing at the same templates/ directory
used by the web app, so templates share the same file tree.

Usage:
    from .email_templates import render_email_template, EMAIL_TEMPLATE_REGISTRY

    html = render_email_template("emails/magic_link.html", lang="en", link=link, expiry_minutes=15)
"""

from pathlib import Path

import jinja2

from .core import config, utcnow
from .i18n import get_translations

_TEMPLATES_DIR = Path(__file__).parent / "templates"

# Standalone environment — not tied to Quart's request context.
# autoescape=True: user-supplied data (names, emails, messages) is auto-escaped.
# Trusted HTML sections use the `| safe` filter explicitly.
_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader(str(_TEMPLATES_DIR)),
    autoescape=True,
    undefined=jinja2.StrictUndefined,
)


def _tformat(s: str, **kwargs) -> str:
    """Jinja filter: interpolate {placeholders} into a translation string.

    Mirrors the `tformat` filter registered in app.py so email templates
    and web templates use the same syntax:
        {{ t.some_key | tformat(name=supplier.name, count=n) }}
    """
    if not kwargs:
        return s
    return s.format(**kwargs)


_env.filters["tformat"] = _tformat


def render_email_template(template_name: str, lang: str = "en", **kwargs) -> str:
    """Render an email template with standard context injected.

    Args:
        template_name: Path relative to templates/ (e.g. "emails/magic_link.html").
        lang: Language code ("en" or "de"). Used for translations + html lang attr.
        **kwargs: Additional context variables passed to the template.

    Returns:
        Rendered HTML string containing a full <!DOCTYPE html> document.
    """
    assert lang in ("en", "de"), f"Unsupported lang: {lang!r}"
    assert template_name.startswith("emails/"), f"Expected emails/ prefix: {template_name!r}"

    translations = get_translations(lang)
    year = utcnow().year

    # Pre-interpolate footer strings so templates don't need to call tformat on them.
    tagline = translations.get("email_footer_tagline", "")
    copyright_text = translations.get("email_footer_copyright", "").format(
        year=year, app_name=config.APP_NAME
    )

    context = {
        "lang": lang,
        "app_name": config.APP_NAME,
        "base_url": config.BASE_URL,
        "t": translations,
        "tagline": tagline,
        "copyright_text": copyright_text,
        **kwargs,
    }

    tmpl = _env.get_template(template_name)
    rendered = tmpl.render(**context)

    assert "<!DOCTYPE html>" in rendered, f"Template {template_name!r} must produce a DOCTYPE document"
    assert "padelnomics" in rendered.lower(), f"Template {template_name!r} must include the wordmark"
    return rendered


# =============================================================================
# Template registry — used by admin gallery for sample preview rendering
# =============================================================================

def _magic_link_sample(lang: str) -> dict:
    return {
        "link": f"{config.BASE_URL}/auth/verify?token=sample_token_abc123",
        "expiry_minutes": 15,
        "preheader": get_translations(lang).get("email_magic_link_preheader", "").format(expiry_minutes=15),
    }


def _quote_verification_sample(lang: str) -> dict:
    t = get_translations(lang)
    court_count = "4"
    return {
        "link": f"{config.BASE_URL}/{lang}/leads/verify?token=verify123&lead=lead456",
        "first_name": "Alex",
        "court_count": court_count,
        "facility_type": "Indoor Padel Club",
        "country": "Germany",
        "recap_parts": ["4 courts", "Indoor Padel Club", "Germany"],
        "preheader": t.get("email_quote_verify_preheader_courts", "").format(court_count=court_count),
    }


def _welcome_sample(lang: str) -> dict:
    t = get_translations(lang)
    return {
        "first_name": "Maria",
        "preheader": t.get("email_welcome_preheader", ""),
    }


def _waitlist_supplier_sample(lang: str) -> dict:
    t = get_translations(lang)
    return {
        "plan_name": "Growth",
        "preheader": t.get("email_waitlist_supplier_preheader", ""),
    }


def _waitlist_general_sample(lang: str) -> dict:
    t = get_translations(lang)
    return {
        "preheader": t.get("email_waitlist_general_preheader", ""),
    }


def _lead_matched_sample(lang: str) -> dict:
    t = get_translations(lang)
    return {
        "first_name": "Thomas",
        "facility_type": "padel",
        "court_count": "6",
        "country": "Austria",
        "preheader": t.get("email_lead_matched_preheader", ""),
    }


def _lead_forward_sample(lang: str) -> dict:
    return {
        "heat": "HOT",
        "country": "Spain",
        "courts": "8",
        "budget": "450000",
        "facility_type": "Outdoor Padel Club",
        "timeline": "Q3 2025",
        "contact_email": "ceo@padelclub.es",
        "contact_name": "Carlos Rivera",
        "contact_phone": "+34 612 345 678",
        "contact_company": "PadelClub Madrid SL",
        "stakeholder_type": "Developer / Investor",
        "build_context": "New build",
        "glass_type": "Panoramic",
        "lighting_type": "LED",
        "location": "Madrid",
        "location_status": "Site confirmed",
        "financing_status": "Self-financed",
        "services_needed": "Full turnkey construction",
        "additional_info": "Seeking experienced international suppliers only.",
        "cta_url": f"{config.BASE_URL}/suppliers/leads/cta/sample_cta_token",
        "preheader": "Outdoor Padel Club project · Q3 2025 timeline — contact details inside",
        "brief_rows": [
            ("Facility", "Outdoor Padel Club (New build)"),
            ("Courts", "8 | Glass: Panoramic | Lighting: LED"),
            ("Location", "Madrid, Spain"),
            ("Timeline", "Q3 2025 | Budget: €450000"),
            ("Phase", "Site confirmed | Financing: Self-financed"),
            ("Services", "Full turnkey construction"),
            ("Additional Info", "Seeking experienced international suppliers only."),
        ],
    }


def _lead_match_notify_sample(lang: str) -> dict:
    return {
        "heat": "WARM",
        "country": "Netherlands",
        "courts": "4",
        "facility_type": "Indoor Padel",
        "timeline": "Q1 2026",
        "credit_cost": 2,
        "preheader": "New matching lead in Netherlands",
    }


def _weekly_digest_sample(lang: str) -> dict:
    return {
        "leads": [
            {"heat": "HOT", "facility_type": "Outdoor Padel", "court_count": "6", "country": "Germany", "timeline": "Q2 2025"},
            {"heat": "WARM", "facility_type": "Indoor Club", "court_count": "4", "country": "Austria", "timeline": "Q3 2025"},
            {"heat": "COOL", "facility_type": "Padel Centre", "court_count": "8", "country": "Switzerland", "timeline": "2026"},
        ],
        "preheader": "3 new leads matching your service area",
    }


def _business_plan_sample(lang: str) -> dict:
    t = get_translations(lang)
    return {
        "download_url": f"{config.BASE_URL}/planner/export/sample_export_token",
        "quote_url": f"{config.BASE_URL}/{lang}/leads/quote",
        "preheader": t.get("email_business_plan_preheader", ""),
    }


def _admin_compose_sample(lang: str) -> dict:
    return {
        "body_html": "<p>Hello,</p><p>This is a test message from the admin compose panel.</p><p>Best regards,<br>Padelnomics Team</p>",
        "preheader": "Test message from admin",
    }


# Registry entry shape:
#   template: path relative to templates/
#   label: human-readable name shown in gallery
#   description: one-line description
#   email_type: email_type value stored in email_log (for cross-linking)
#   sample_data: callable(lang) → dict of template context
EMAIL_TEMPLATE_REGISTRY: dict[str, dict] = {
    "magic_link": {
        "template": "emails/magic_link.html",
        "label": "Magic Link",
        "description": "Passwordless sign-in link sent to users requesting access.",
        "email_type": "magic_link",
        "sample_data": _magic_link_sample,
    },
    "quote_verification": {
        "template": "emails/quote_verification.html",
        "label": "Quote Verification",
        "description": "Email address verification for new project quote requests.",
        "email_type": "quote_verification",
        "sample_data": _quote_verification_sample,
    },
    "welcome": {
        "template": "emails/welcome.html",
        "label": "Welcome",
        "description": "Sent to new users after their first successful sign-in.",
        "email_type": "welcome",
        "sample_data": _welcome_sample,
    },
    "waitlist_supplier": {
        "template": "emails/waitlist_supplier.html",
        "label": "Waitlist — Supplier",
        "description": "Confirmation for suppliers who joined the Growth/Pro waitlist.",
        "email_type": "waitlist",
        "sample_data": _waitlist_supplier_sample,
    },
    "waitlist_general": {
        "template": "emails/waitlist_general.html",
        "label": "Waitlist — General",
        "description": "Confirmation for general sign-up waitlist submissions.",
        "email_type": "waitlist",
        "sample_data": _waitlist_general_sample,
    },
    "lead_matched": {
        "template": "emails/lead_matched.html",
        "label": "Lead Matched",
        "description": "Notifies the project owner that suppliers are now reviewing their brief.",
        "email_type": "lead_matched",
        "sample_data": _lead_matched_sample,
    },
    "lead_forward": {
        "template": "emails/lead_forward.html",
        "label": "Lead Forward",
        "description": "Full project brief sent to a supplier after they unlock a lead.",
        "email_type": "lead_forward",
        "sample_data": _lead_forward_sample,
    },
    "lead_match_notify": {
        "template": "emails/lead_match_notify.html",
        "label": "Lead Match Notify",
        "description": "Notifies matching suppliers that a new lead is available in their area.",
        "email_type": "lead_match_notify",
        "sample_data": _lead_match_notify_sample,
    },
    "weekly_digest": {
        "template": "emails/weekly_digest.html",
        "label": "Weekly Digest",
        "description": "Monday digest of new leads matching a supplier's service area.",
        "email_type": "weekly_digest",
        "sample_data": _weekly_digest_sample,
    },
    "business_plan": {
        "template": "emails/business_plan.html",
        "label": "Business Plan Ready",
        "description": "Notifies the user when their business plan PDF export is ready.",
        "email_type": "business_plan",
        "sample_data": _business_plan_sample,
    },
    "admin_compose": {
        "template": "emails/admin_compose.html",
        "label": "Admin Compose",
        "description": "Branded wrapper used for ad-hoc emails sent from the compose panel.",
        "email_type": "admin_compose",
        "sample_data": _admin_compose_sample,
    },
}
