from decimal import Decimal

from app.services.email_templates import render_email

_OTP_CONTEXT = {
    "subject": "Verify your email — YENEB CODS",
    "heading": "Verify your email",
    "intro_text": "Enter this code to finish creating your YENEB account.",
    "code": "482913",
    "expiry_minutes": 5,
    "name": "Abebe",
}


def test_otp_template_includes_code_expiry_and_name_in_both_parts():
    html, text = render_email("otp", _OTP_CONTEXT)
    for part in (html, text):
        assert "482913" in part
        assert "5 minute" in part
        assert "Abebe" in part


def test_otp_template_omits_greeting_when_name_is_none():
    context = {**_OTP_CONTEXT, "name": None}
    html, text = render_email("otp", context)
    assert "Hi None" not in html
    assert "Hi None" not in text


def test_every_email_template_includes_the_logo_and_brand_colors():
    html, _text = render_email("otp", _OTP_CONTEXT)
    assert "#FDEA12" in html  # brand yellow
    assert "res.cloudinary.com" in html  # logo, from settings.email_logo_url


def test_welcome_template_greets_by_name():
    html, text = render_email(
        "welcome",
        {"subject": "Welcome", "name": "Marta", "app_download_url": ""},
    )
    assert "Marta" in html
    assert "Marta" in text


def test_welcome_template_hides_download_button_without_a_url():
    html, _text = render_email(
        "welcome", {"subject": "Welcome", "name": "Marta", "app_download_url": ""}
    )
    assert "Get the app" not in html


def test_order_confirmation_template_shows_items_and_total():
    html, text = render_email(
        "order_confirmation",
        {
            "subject": "Order confirmed",
            "name": "Marta",
            "branch_name": "Bole Branch",
            "eta_label": "Estimated delivery",
            "eta_text": "30–45 min",
            "items": [
                {
                    "name": "Beef Burger",
                    "quantity": Decimal("2"),
                    "line_total": Decimal("300.00"),
                }
            ],
            "subtotal": Decimal("300.00"),
            "delivery_fee": Decimal("25.00"),
            "service_fee": Decimal("15.00"),
            "vat": Decimal("48.75"),
            "discount_amount": Decimal("0.00"),
            "total": Decimal("388.75"),
            "delivery_address": "Bole, Addis Ababa",
        },
    )
    for part in (html, text):
        assert "Bole Branch" in part
        assert "Beef Burger" in part
        assert "388.75" in part
        assert "Bole, Addis Ababa" in part


def test_order_confirmation_template_hides_delivery_fee_row_when_zero():
    html, text = render_email(
        "order_confirmation",
        {
            "subject": "Order confirmed",
            "name": "Marta",
            "branch_name": "Bole Branch",
            "eta_label": "Ready for pickup in",
            "eta_text": "15-20 min",
            "items": [],
            "subtotal": Decimal("0.00"),
            "delivery_fee": Decimal("0.00"),
            "service_fee": Decimal("0.00"),
            "vat": Decimal("0.00"),
            "discount_amount": Decimal("0.00"),
            "total": Decimal("0.00"),
            "delivery_address": None,
        },
    )
    assert "Delivery fee" not in html
    assert "Delivery fee" not in text
    assert "Delivering to" not in html
