"""
One-off helper to register the webhook URL with Telegram.

Two ways to run this:

  A) Locally (if you have Python + token on your machine):
        python -m financegpt_bot.setup_webhook

  B) Via browser on the server:
        Add REGISTER_WEBHOOK=true to your .env, then open:
        https://yourdomain.com/setup-webhook
        Remove REGISTER_WEBHOOK=true from .env afterwards.

The /setup-webhook route is only active when REGISTER_WEBHOOK=true so it is
not accidentally reachable in normal operation.
"""

import sys

from . import config
from .services.telegram_api import TelegramAPI


def register() -> dict:
    api = TelegramAPI()
    result = api.set_webhook(config.WEBHOOK_URL)
    return result


def main() -> None:
    result = register()
    print("setWebhook ->", result)


# --- Optional in-app route (activated via REGISTER_WEBHOOK=true in .env) ----

def register_route(app) -> None:
    """Call this from passenger_wsgi.py or app.py to add the setup route."""
    from flask import jsonify

    @app.route("/setup-webhook", methods=["GET"])
    def setup_webhook_route():
        if config._env("REGISTER_WEBHOOK", "false").lower() != "true":
            return jsonify({"error": "disabled"}), 403
        result = register()
        return jsonify(result), 200


if __name__ == "__main__":
    main()
