"""Run once after deployment and whenever WEBHOOK_URL changes."""
from __future__ import annotations

import asyncio

from aiogram import Bot

from config import Settings


async def main() -> None:
    settings = Settings.from_env()
    if not settings.webhook_url:
        raise RuntimeError("WEBHOOK_URL is missing from .env")
    if not settings.webhook_url.startswith("https://"):
        raise RuntimeError("WEBHOOK_URL must start with https://")
    if not settings.webhook_secret:
        raise RuntimeError("WEBHOOK_SECRET is missing from .env")

    bot = Bot(settings.bot_token)
    try:
        await bot.set_webhook(
            url=settings.webhook_url,
            secret_token=settings.webhook_secret,
            drop_pending_updates=False,
        )
        info = await bot.get_webhook_info()
        print(f"Webhook URL: {info.url}")
        print(f"Pending updates: {info.pending_update_count}")
        print(f"Last error: {info.last_error_message or '-'}")
    finally:
        await bot.session.close()


if __name__ == "__main__":
    asyncio.run(main())

