"""INV-FIX-002 / INV-AUD-011: the HTML e-mail built from a public lead escapes visitor input.

Pure unit tests of build_email_content; nothing is sent.
"""
from datetime import datetime

import pytest

from InvestmentWebsite.offers.platform_facade import PLATFORM_MAP, build_email_content

NOW = datetime(2026, 10, 19, 12, 0, 0)
INJECTION = '<a href="https://phish.example/login">Zaloguj się</a><script>alert(1)</script>'


def _payload(**values):
    payload = {
        "phone": "600 100 200", "email": "jan.kowalski@example.com", "message": "Dzień dobry, proszę o kontakt.",
        "offer_url": "https://tripinvest.pl/oferta/apartament-75156",
        "platform_page_url": "https://investment-nieruchomosci.com/nieruchomosci/hiszpania/oferta/abc",
        "ip": "203.0.113.5", "user_agent": "Mozilla/5.0", "platform": "tripinvest",
    }
    payload.update(values)
    return payload


def _html(**values):
    return build_email_content(NOW, _payload(**values), PLATFORM_MAP["tripinvest"])["html_body"]


def test_normal_lead_renders_contact_data_and_both_links():
    content = build_email_content(NOW, _payload(), PLATFORM_MAP["tripinvest"])
    html = content["html_body"]

    assert content["subject"] == "🌍 Nowe zapytanie – TripInvest"
    assert content["reply_to"] == "jan.kowalski@example.com"
    assert "<td>600 100 200</td>" in html
    assert "<td>jan.kowalski@example.com</td>" in html
    assert "Dzień dobry, proszę o kontakt." in html
    assert '<a href="https://tripinvest.pl/oferta/apartament-75156"' in html
    assert '<a href="https://investment-nieruchomosci.com/nieruchomosci/hiszpania/oferta/abc"' in html
    assert "Oferta biura partnerskiego" in html and "Zobacz ofertę na naszej platformie" in html
    assert "<strong>IP:</strong> 203.0.113.5</p>" in html
    # Plain-text part keeps the raw values (no HTML there).
    assert "Telefon: 600 100 200" in content["text_body"]


@pytest.mark.parametrize("field", ["message", "phone", "email", "user_agent", "ip"])
def test_visitor_fields_cannot_inject_markup(field):
    html = _html(**{field: INJECTION})

    assert "<script>" not in html
    assert "phish.example/login\">" not in html
    assert "&lt;script&gt;alert(1)&lt;/script&gt;" in html


def test_query_string_ampersand_stays_a_valid_link():
    html = _html(offer_url="https://tripinvest.pl/oferta/x-1?lang=pl&ref=crm")
    assert '<a href="https://tripinvest.pl/oferta/x-1?lang=pl&amp;ref=crm"' in html


@pytest.mark.parametrize("url", [
    "javascript:alert(1)",
    "data:text/html,<b>x</b>",
    '"><img src=x onerror=alert(1)>',
    "https://tripinvest.pl@phish.example/oferta",
    "https://user:pass@phish.example/",
    "//phish.example/x",
    "/relative",
])
def test_unsafe_link_targets_are_not_rendered_as_links(url):
    html = _html(offer_url=url, platform_page_url=url)

    assert "Oferta biura partnerskiego" not in html
    assert "Zobacz ofertę na naszej platformie" not in html
    assert "phish.example" not in html
    assert "javascript:" not in html
