from app.config import Settings
from app.services.bot_config_service import BotConfigService


def _make_settings() -> Settings:
    return Settings.model_construct(
        bot_brand_name="OREOL VPN",
        bot_public_username="oreol_vpn_bot",
        bot_support_url="https://t.me/support_account",
        bot_terms_url="https://example.com/terms",
        bot_support_ticket_link="",
        bot_support_ticket_chat_id_raw="",
        bot_support_ticket_thread_id=0,
        bot_start_image_enabled=True,
        bot_start_image_path="assets/main.png",
        payment_review_link="",
        payment_review_chat_id_raw="",
        payment_review_thread_id=0,
        referral_discount_percent=5,
        referral_bonus_days=7,
        payment_plans_raw="30:250,180:600",
        payment_transfer_text="Реквизиты",
        payment_support_text="Напишите в поддержку",
        payment_success_text="Доступ выдан",
        payment_plan_traffic_limit_gb=0,
        payment_plan_traffic_reset_period="NO_RESET",
        payment_internal_squad_uuids_raw="uuid-1",
        payment_external_squad_uuid="",
        payment_username_prefix="Oreol",
        payment_user_tag="BOT",
    )


async def test_bot_config_service_updates_runtime_snapshot(session_factory) -> None:
    service = BotConfigService(
        settings=_make_settings(),
        session_factory=session_factory,
    )

    await service.update_setting(
        key="bot_brand_name",
        raw_value="NEW BRAND",
        updated_by_telegram_id=1,
    )
    await service.update_setting(
        key="payment_plans_raw",
        raw_value="30:300,90:700",
        updated_by_telegram_id=1,
    )
    await service.update_setting(
        key="referral_enabled",
        raw_value="off",
        updated_by_telegram_id=1,
    )
    await service.update_setting(
        key="payment_success_text",
        raw_value="Новый текст после оплаты",
        updated_by_telegram_id=1,
    )

    snapshot = await service.get_snapshot()

    assert snapshot.bot_brand_name == "NEW BRAND"
    assert snapshot.referral_enabled is False
    assert snapshot.payment_success_text == "Новый текст после оплаты"
    assert [(plan.days, plan.amount_rub) for plan in snapshot.payment_plans] == [
        (30, 300),
        (90, 700),
    ]


async def test_bot_config_service_formats_values_for_admin_ui(session_factory) -> None:
    service = BotConfigService(
        settings=_make_settings(),
        session_factory=session_factory,
    )
    snapshot = await service.get_snapshot()

    assert BotConfigService.format_value(snapshot=snapshot, key="bot_public_username") == "@oreol_vpn_bot"
    assert BotConfigService.format_value(snapshot=snapshot, key="referral_enabled") == "on"
    assert BotConfigService.format_value(snapshot=snapshot, key="payment_plan_traffic_limit_gb") == "0 GB (unlimited)"
