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

from __future__ import annotations

import jingrow
from jingrow import _
from jingrow.model.page import Page

from jcloud.utils.webhook import create_webhook_event


class SitePlanChange(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_plan: DF.Link | None
		site: DF.Link
		team: DF.Link | None
		timestamp: DF.Datetime | None
		to_plan: DF.Link
		type: DF.Literal["", "Initial Plan", "Upgrade", "Downgrade"]
	# end: auto-generated types

	dashboard_fields = ("from_plan", "to_plan", "type", "site", "timestamp")

	def validate(self):
		if not self.from_plan and self.to_plan:
			self.type = "Initial Plan"

		if self.from_plan and not self.type:
			from_plan_value = jingrow.db.get_value("Site Plan", self.from_plan, "price_usd")
			to_plan_value = jingrow.db.get_value("Site Plan", self.to_plan, "price_usd")
			self.type = "Downgrade" if from_plan_value > to_plan_value else "Upgrade"

		if (
			self.from_plan
			and self.to_plan
			and self.type == "Downgrade"
			and not jingrow.db.get_value("Site Plan", self.to_plan, "allow_downgrading_from_other_plan")
		):
			jingrow.throw(f"Sorry, you cannot downgrade to {self.to_plan} from {self.from_plan}")

		if self.type == "Initial Plan":
			self.from_plan = ""

	def after_insert(self):
		if self.team != "Administrator":
			create_webhook_event("Site Plan Change", self, self.team)

		if self.type == "Initial Plan":
			self.create_subscription()
			return

		# move this code to Server Scripts
		# if self.type == "Downgrade":
		# 	last_plan_change = jingrow.get_last_pg(
		# 		"Site Plan Change", filters={"site": self.site, "team": self.team}
		# 	)
		# 	# check if last site plan change was made before 48 hours
		# 	if last_plan_change.creation > jingrow.utils.add_days(None, -2):
		# 		jingrow.throw("Cannot downgrade plan within 48 hours")

		self.change_subscription_plan()

	def create_subscription(self):
		jingrow.get_pg(
			pagetype="Subscription",
			team=self.team,
			document_type="Site",
			document_name=self.site,
			plan_type="Site Plan",
			plan=self.to_plan,
		).insert()

	def change_subscription_plan(self):
		site = jingrow.get_pg("Site", self.site)
		subscription = site.subscription
		if not subscription:
			jingrow.throw(f"No subscription for site {site.name}")

		if self.from_plan and self.from_plan != subscription.plan:
			jingrow.throw(
				_("Site {0} is currently on {1} plan and not {2}").format(
					site.name, subscription.plan, self.from_plan
				)
			)

		subscription.plan = self.to_plan
		subscription.flags.updater_reference = {
			"pagetype": self.pagetype,
			"docname": self.name,
			"label": _("via Site Plan Change"),
		}
		subscription.enabled = 1
		subscription.save()
