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

import jingrow

from jcloud.api import site as site_api
from jcloud.saas.api import whitelist_saas_api


@whitelist_saas_api
def info():
	is_fc_user = False
	site = jingrow.get_value("Site", jingrow.local.site_name, ["plan", "trial_end_date", "team"], as_dict=True)
	site_user = jingrow.request.headers.get("x-site-user")

	team_members = jingrow.get_pg("Team", site.team).get_user_list()
	if site_user and site_user in team_members:
		is_fc_user = True

	return {
		"is_fc_user": is_fc_user,
		"name": jingrow.local.site_name,
		"trial_end_date": site.trial_end_date,
		"plan": jingrow.db.get_value("Site Plan", site.plan, ["is_trial_plan"], as_dict=True)
		if site.plan
		else None,
	}


@whitelist_saas_api
def change_plan(plan: str):
	site = jingrow.local.get_site()
	site.set_plan(plan)


@whitelist_saas_api
def get_plans():
	site = jingrow.get_value("Site", jingrow.local.site_name, ["server", "group", "plan"], as_dict=True)
	is_site_on_private_bench = jingrow.db.get_value("Release Group", site.group, "public") is False
	is_site_on_shared_server = jingrow.db.get_value("Server", site.server, "public")
	plans = site_api.get_site_plans()
	filtered_plans = []

	for plan in plans:
		if plan.name != site.plan:
			if plan.restricted_plan or plan.is_jingrow_plan or plan.is_trial_plan:
				continue
			if is_site_on_private_bench and not plan.private_benches:
				continue
			if plan.dedicated_server_plan and is_site_on_shared_server:
				continue
			if not plan.dedicated_server_plan and not is_site_on_shared_server:
				continue
		filtered_plans.append(plan)

	"""
	plans `site_api.get_site_plans()` doesn't include trial plan, as we don't have any roles specified for trial plan
	because from backend only we set the trial plan, end-user can't subscribe to trial plan directly
	If the site is on a trial plan, add it to the starting of the list
	"""

	current_plan = jingrow.get_pg("Site Plan", site.plan)
	if current_plan.is_trial_plan:
		filtered_plans.insert(
			0,
			{
				"name": current_plan.name,
				"plan_title": current_plan.plan_title,
				"price_usd": current_plan.price_usd,
				"price_cny": current_plan.price_cny,
				"cpu_time_per_day": current_plan.cpu_time_per_day,
				"max_storage_usage": current_plan.max_storage_usage,
				"max_database_usage": current_plan.max_database_usage,
				"database_access": current_plan.database_access,
				"support_included": current_plan.support_included,
				"offsite_backups": current_plan.offsite_backups,
				"private_benches": current_plan.private_benches,
				"monitor_access": current_plan.monitor_access,
				"dedicated_server_plan": current_plan.dedicated_server_plan,
				"is_trial_plan": current_plan.is_trial_plan,
				"allow_downgrading_from_other_plan": False,
				"clusters": [],
				"allowed_apps": [],
				"bench_versions": [],
				"restricted_plan": False,
			},
		)

	return filtered_plans


@whitelist_saas_api
def get_first_support_plan():
	plans = get_plans()
	for plan in plans:
		if plan.support_included and not plan.is_trial_plan:
			return plan
	return None
