from datetime import datetime, timedelta

from app.utils.formatters import (
    build_help_text,
    format_bytes,
    format_datetime,
    format_datetime_with_days_left,
    format_issued_access,
    format_payment_plan,
    format_traffic_limit,
)


def test_format_bytes_for_megabytes() -> None:
    assert format_bytes(1024 * 1024) == "1.00 MB"


def test_format_traffic_limit_without_limit() -> None:
    assert format_traffic_limit(0) == "без лимита"


def test_format_datetime_value() -> None:
    assert format_datetime(datetime(2026, 4, 21, 12, 30)) == "21.04.2026 12:30"


def test_build_help_text_contains_only_start_command() -> None:
    text = build_help_text(is_admin=False)

    assert "/start - открыть главное меню" in text
    assert "Дальше используйте кнопки внутри интерфейса бота." in text
    assert "/menu - " not in text
    assert "/buy - " not in text
    assert "/paysupport - " not in text
    assert "/sync - " not in text
    assert "/link " not in text
    assert "/help - " not in text


def test_format_datetime_with_days_left() -> None:
    value = datetime.now().replace(microsecond=0) + timedelta(days=3, hours=2)

    text = format_datetime_with_days_left(value)

    assert value.strftime("%d.%m.%Y %H:%M") in text
    assert "(4 " in text


def test_format_payment_plan_uses_rubles() -> None:
    text = format_payment_plan(
        title="OREOL VPN на 30 дней",
        description="Доступ к VPN на 30 дней",
        price_stars=250,
        duration_days=30,
        traffic_limit_bytes=0,
    )

    assert "OREOL VPN на 30 дней" in text
    assert "Цена: <b>250 ₽</b>" in text
    assert "Трафик: <b>без лимита</b>" in text


def test_format_issued_access_contains_subscription_data() -> None:
    text = format_issued_access(
        subscription_url="https://example.com/sub",
        expire_at=datetime(2026, 5, 21, 18, 0),
        traffic_limit_bytes=0,
        brand_name="OREOL VPN",
    )

    assert "OREOL VPN" in text
    assert "https://example.com/sub" in text
    assert "21.05.2026 18:00" in text
    assert "Подписка активирована" in text
    assert "Что делать дальше" in text
    assert "/me</code>" not in text


def test_format_issued_access_includes_custom_success_text() -> None:
    text = format_issued_access(
        subscription_url="https://example.com/sub",
        expire_at=datetime(2026, 5, 21, 18, 0),
        traffic_limit_bytes=0,
        brand_name="OREOL VPN",
        success_text="Откройте приложение и импортируйте ссылку <важно>.",
    )

    assert "Откройте приложение и импортируйте ссылку" in text
    assert "&lt;важно&gt;" in text


def test_format_issued_access_for_renewal_mentions_extension() -> None:
    text = format_issued_access(
        subscription_url="https://example.com/sub",
        expire_at=datetime(2026, 5, 21, 18, 0),
        traffic_limit_bytes=0,
        brand_name="OREOL VPN",
        is_renewal=True,
    )

    assert "подписка продлена" in text.lower()
    assert "Подписка продлена." in text
