from __future__ import absolute_import

import click
import jingrow
from jingrow.commands import get_site, pass_context


@click.command("ngrok-webhook")
@pass_context
def start_ngrok_and_set_webhook(context):
	from pyngrok import ngrok

	from jcloude.api.billing import get_stripe

	site = get_site(context)
	jingrow.init(site=site)
	jingrow.connect()

	# Set ngrok auth token
	auth_token = jingrow.db.get_single_value("Press Settings", "ngrok_auth_token")

	if auth_token:
		ngrok.set_auth_token(auth_token)

	port = jingrow.conf.http_port or jingrow.conf.webserver_port
	tunnel = ngrok.connect(port, host_header=site)
	public_url = tunnel.public_url
	print()
	print(f"{public_url} -> http://{site}:{port}")
	print(f"Inspect logs at {tunnel.api_url}")

	stripe = get_stripe()
	url = f"{public_url}/api/method/jcloude.jcloude.doctype.stripe_webhook_log.stripe_webhook_log.stripe_webhook_handler"
	stripe.WebhookEndpoint.modify(
		jingrow.db.get_single_value("Press Settings", "stripe_webhook_endpoint_id"), url=url
	)
	print("Updated Stripe Webhook Endpoint")

	ngrok_process = ngrok.get_ngrok_process()
	try:
		# Block until CTRL-C or some other terminating event
		ngrok_process.proc.wait()
	except KeyboardInterrupt:
		print("Shutting down server...")
		jingrow.destroy()
		ngrok.kill()


commands = [
	start_ngrok_and_set_webhook,
]
