# Copyright (c) 2024, JINGROW
# For license information, please see license.txt

from __future__ import annotations

import contextlib
import ipaddress
import json
from urllib.parse import urlparse

import jingrow
import jingrow.query_builder
import jingrow.query_builder.functions
import requests
from jingrow.model.page import Page

from jcloud.api.client import dashboard_whitelist
from jcloud.overrides import get_permission_query_conditions_for_pagetype
from jcloud.utils import is_valid_hostname


class JcloudWebhook(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		from jcloud.jcloud.pagetype.jcloud_webhook_selected_event.jcloud_webhook_selected_event import (
			JcloudWebhookSelectedEvent,
		)

		enabled: DF.Check
		endpoint: DF.Data
		events: DF.Table[JcloudWebhookSelectedEvent]
		secret: DF.Data
		team: DF.Link
	# end: auto-generated types

	PAGETYPE = "Jcloud Webhook"
	dashboard_fields = ("enabled", "endpoint", "events")

	def validate(self):
		# maximum 5 webhooks per team
		if self.is_new() and jingrow.db.count("Jcloud Webhook", {"team": self.team}) > 5:
			jingrow.throw("You have reached the maximum number of webhooks per team")

		if self.has_value_changed("endpoint"):
			self.enabled = 0
		# should have atleast one event selected
		if not self.events:
			jingrow.throw("At least one event should be selected")
		# validate endpoint url format
		self.validate_endpoint_url_format()
		# check for duplicate webhooks
		webhooks = jingrow.get_all(
			"Jcloud Webhook",
			filters={"team": self.team, "endpoint": self.endpoint, "name": ("!=", self.name)},
			pluck="name",
		)
		if len(webhooks) != 0:
			jingrow.throw("You have already added webhook for this endpoint")

	def validate_endpoint_url_format(self):
		url = urlparse(self.endpoint)
		if not url.netloc:
			jingrow.throw("Endpoint should be a valid url")

		# protocol should be http or https
		if url.scheme not in ["http", "https"]:
			jingrow.throw("Endpoint should start with http:// or https://")

		# dont allow query params
		if url.query:
			jingrow.throw("Endpoint should not have query params")

		isIPAddress = False
		# If endpoint target is ip address, it should be a public ip address
		with contextlib.suppress(ValueError):
			ip = ipaddress.ip_address(url.hostname)
			isIPAddress = True
			if not ip.is_global:
				jingrow.throw("Endpoint address should be a public ip or domain")

		if not isIPAddress:
			# domain should be a fqdn
			if not is_valid_hostname(url.hostname):
				jingrow.throw("Endpoint address should be a valid domain")

			# Endpoint can't be any local domain
			if not jingrow.conf.developer_mode and ("localhost" in url.hostname or ".local" in url.hostname):
				jingrow.throw("Endpoint can't be localhost or local domain")

	@dashboard_whitelist()
	def validate_endpoint(self) -> dict:
		response = ""
		response_status_code = 0
		payload = {"event": "Webhook Validate", "data": {}}
		try:
			req = requests.post(
				self.endpoint,
				timeout=5,
				json=payload,
				headers={"X-Webhook-Secret": self.secret},
			)
			response = req.text or ""
			response_status_code = req.status_code
		except requests.exceptions.ConnectionError:
			response = "Failed to connect to the webhook endpoint"
		except requests.exceptions.SSLError:
			response = "SSL Error. Please check if SSL the certificate of the webhook is valid."
		except (requests.exceptions.Timeout, requests.exceptions.ConnectTimeout):
			response = "Request Timeout. Please check if the webhook is reachable."
		except Exception as e:
			response = str(e)

		return jingrow._dict(
			{
				"success": response_status_code >= 200 and response_status_code < 300,
				"request": json.dumps(payload, indent=2),
				"response": response,
				"response_status_code": response_status_code,
			}
		)

	@dashboard_whitelist()
	def activate(self):
		result = self.validate_endpoint()
		if result.get("success"):
			self.enabled = 1
			self.save()
			jingrow.msgprint("Webhook activated successfully")
		else:
			message = f"<b>Status Code -</b> {result.response_status_code}</br><b>Response -</b></br>{result.response}"
			jingrow.throw(title="Webhook endpoint is invalid", msg=message)

	@dashboard_whitelist()
	def disable(self):
		self.enabled = False
		self.save()

	@dashboard_whitelist()
	def disable_and_notify(self):
		self.disable()
		email = jingrow.db.get_value("Team", self.team, "user")
		if not email:
			return
		if jingrow.conf.developer_mode:
			print(f"Emailing {email}")
			print(f"{self.name} webhook has been disabled")
			return

		jingrow.sendmail(
			recipients=email,
			subject="Important: Your Configured Webhook on Jingrow is disabled",
			template="jcloud_webhook_disabled",
			args={"endpoint": self.endpoint},
			now=True,
		)

	@dashboard_whitelist()
	def delete(self):
		jingrow.db.sql("delete from `tabJcloud Webhook Attempt` where webhook = %s", (self.name,))
		jingrow.delete_pg("Jcloud Webhook", self.name)


get_permission_query_conditions = get_permission_query_conditions_for_pagetype("Site")


def auto_disable_high_delivery_failure_webhooks():
	# In past hour, if 70% of webhook deliveries has failed, disable the webhook and notify the user
	data = jingrow.db.sql(
		"""
SELECT `endpoint`
FROM `tabJcloud Webhook Attempt`
WHERE `creation` >= NOW() - INTERVAL 1 HOUR
GROUP BY `endpoint`
HAVING (COUNT(CASE WHEN `status` = 'Failed' THEN 1 END) / COUNT(*)) * 100 > 70;
""",
		as_dict=True,
	)
	endpoints = [row.endpoint for row in data]
	pg_names = jingrow.get_all("Jcloud Webhook", filters={"endpoint": ("in", endpoints)}, pluck="name")
	for pg_name in pg_names:
		pg = jingrow.get_pg("Jcloud Webhook", pg_name)
		pg.disable_and_notify()
