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

from __future__ import annotations

import json

import jingrow
import requests
from jingrow.model.page import Page
from jingrow.utils import add_to_date, now

from jcloud.overrides import get_permission_query_conditions_for_pagetype


class JcloudWebhookLog(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_attempt.jcloud_webhook_attempt import JcloudWebhookAttempt

		attempts: DF.Table[JcloudWebhookAttempt]
		event: DF.Link
		next_retry_at: DF.Datetime | None
		request_payload: DF.JSON
		retries: DF.Int
		status: DF.Literal["Pending", "Queued", "Sent", "Partially Sent", "Failed"]
		team: DF.Link
	# end: auto-generated types

	def validate(self):
		if not self.next_retry_at:
			self.next_retry_at = jingrow.utils.now()

	def _send_webhook_call(self, webhook_name, payload, url, secret, save: bool = True) -> bool:
		response = ""
		response_status_code = 0
		try:
			req = requests.post(
				url,
				json=payload,
				headers={"X-Webhook-Secret": secret},
				timeout=5,
			)
			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)

		sent = response_status_code >= 200 and response_status_code < 300

		self.append(
			"attempts",
			{
				"endpoint": url,
				"webhook": webhook_name,
				"status": "Sent" if sent else "Failed",
				"response_body": response,
				"response_status_code": response_status_code,
				"timestamp": jingrow.utils.now(),
			},
		)
		if save:
			self.save()

		return sent

	def schedule_retry(self, save: True):
		self.retries = self.retries + 1
		self.next_retry_at = add_to_date(now(), minutes=2**self.retries)
		if save:
			self.save()

	def send(self):
		if len(self.attempts) == 0:
			self._process_webhook_call()
			return

		# Try failed attempts
		self._retry_failed_attempts()

	def _process_webhook_call(self):
		try:
			JcloudWebhookSelectedEvent = jingrow.qb.PageType("Jcloud Webhook Selected Event")
			JcloudWebhook = jingrow.qb.PageType("Jcloud Webhook")
			query = (
				jingrow.qb.from_(JcloudWebhookSelectedEvent)
				.select(JcloudWebhook.name, JcloudWebhook.endpoint, JcloudWebhook.secret)
				.left_join(JcloudWebhook)
				.on(JcloudWebhookSelectedEvent.parent == JcloudWebhook.name)
				.where(JcloudWebhookSelectedEvent.event == self.event)
				.where(JcloudWebhook.team == self.team)
				.where(JcloudWebhook.enabled == 1)
			)
			webhooks = query.run(as_dict=True)
			payload = json.loads(self.request_payload)
			total = len(webhooks)
			sent = 0
			for webhook in webhooks:
				isSent = self._send_webhook_call(
					webhook.name,
					payload,
					webhook.endpoint,
					webhook.secret,
					save=False,
				)
				if isSent:
					sent += 1

			if total == 0:
				self.status = "Sent"
			else:
				if sent == total:
					self.status = "Sent"
				elif sent != total and sent != 0:
					self.status = "Partially Sent"
				else:
					self.status = "Failed"
					self.schedule_retry(save=False)
		except Exception:
			self.status = "Failed"
			self.schedule_retry(save=False)

		self.save()

	def _retry_failed_attempts(self):
		webhook_call_status = jingrow._dict()
		for record in self.attempts:
			if record.status == "Failed" and webhook_call_status.get(record.webhook, "") != "Sent":
				webhook_call_status[record.webhook] = "Failed"
			if record.status == "Sent":
				webhook_call_status[record.webhook] = "Sent"

		# filter out webhooks that need to be retried
		webhooks_to_retry = [
			webhook for webhook in webhook_call_status if webhook_call_status[webhook] == "Failed"
		]

		sent = 0
		payload = json.loads(self.request_payload)

		for webhook in webhooks_to_retry:
			webhook_data = jingrow.get_value(
				"Jcloud Webhook", record.webhook, ["endpoint", "secret"], as_dict=True
			)
			is_sent = self._send_webhook_call(
				webhook,
				payload,
				webhook_data.endpoint,
				webhook_data.secret,
			)
			if is_sent:
				sent += 1

		if len(webhooks_to_retry) == 0 or sent == len(webhooks_to_retry):
			self.status = "Sent"
		elif (len(webhook_call_status) - len(webhooks_to_retry) > 0) or sent > 0:
			self.status = "Partially Sent"
			self.schedule_retry(save=False)
		else:
			self.status = "Failed"
			self.schedule_retry(save=False)

		self.save()


get_permission_query_conditions = get_permission_query_conditions_for_pagetype("Jcloud Webhook Log")


def process():
	records = jingrow.get_all(
		"Jcloud Webhook Log",
		filters={
			"status": ["in", ["Pending", "Failed", "Partially Sent"]],
			"retries": ["<=", 3],
			"next_retry_at": ["<=", jingrow.utils.now()],
		},
		pluck="name",
		limit=100,
	)
	# set status of these records to Queued
	jingrow.db.set_value("Jcloud Webhook Log", {"name": ("in", records)}, "status", "Queued")
	# enqueue these records
	for record in records:
		jingrow.enqueue_pg(
			"Jcloud Webhook Log",
			record,
			method="send",
			queue="default",
			job_id=f"jcloud_webhook_log:{record}",
			deduplicate=True,
		)


def clean_logs_older_than_24_hours():
	names = jingrow.get_all(
		"Jcloud Webhook Log", filters={"creation": ["<", jingrow.utils.add_days(None, -1)]}, pluck="name"
	)
	jingrow.delete_pg("Jcloud Webhook Log", names)
