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

import jingrow
from jingrow.model.page import Page

from jcloud.saas.pagetype.saas_app_plan.saas_app_plan import get_app_plan_features
from jcloud.utils import get_current_team


class SaasApp(Page):
	def autoname(self):
		self.name = self.app

	def get_app_versions(self):
		v = [ver.version_name for ver in self.app_versions]
		return v

	def get_plans(self, site):
		return get_plans_for_app(self.name, site)


def get_plans_for_app(app, site):
	plans = []

	filters = {"app": app, "enabled": 1}

	if app == "jerp_smb":
		filters.update({"is_us_eu": get_current_team(get_pg=True).is_us_eu})

	saas_app_plans = jingrow.get_all(
		"Saas App Plan",
		filters=filters,
		fields=[
			"name",
			"plan",
			"plan_title",
			"is_free",
			"app",
			"gst_inclusive",
			"annual_discount",
			"annual_discount_cny",
			"annual_discount_usd",
		],
	)

	for app_plan in saas_app_plans:
		plan_data = {}
		plan_data.update(app_plan)

		plan_prices = get_plan_prices(app_plan.plan)
		plan_data.update(plan_prices)

		plan_data["features"] = get_app_plan_features(app_plan.name)
		selected_plan = get_selected_plan(app, site)
		plan_data["is_selected"] = selected_plan == app_plan.name

		plans.append(plan_data)

	plans.sort(key=lambda x: x["price_usd"])

	return plans


def get_plan_prices(plan_name):
	plan_prices = jingrow.db.get_value(
		"Site Plan", plan_name, ["plan_title", "price_usd", "price_cny"], as_dict=True
	)

	return plan_prices


def get_selected_plan(app, site):
	sub_name = jingrow.get_all(
		"Saas App Subscription", {"app": app, "site": site}, pluck="saas_app_plan"
	)

	return sub_name[0]
