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


import jingrow

from jcloud.utils import get_default_team_for_user, get_valid_teams_for_user

base_template_path = "templates/www/dashboard.html"
no_cache = 1


def get_context():
	return _get_context()


def _get_context():
	csrf_token = jingrow.sessions.get_csrf_token()
	jingrow.db.commit()
	context = jingrow._dict()
	context.boot = get_boot()
	context.boot.csrf_token = csrf_token
	return context


@jingrow.whitelist(methods=["GET", "POST"], allow_guest=True)
def get_context_for_dev():
	if not jingrow.conf.developer_mode:
		jingrow.throw("This method is only meant for developer mode")
	csrf_token = jingrow.sessions.get_csrf_token()
	jingrow.db.commit()
	boot = get_boot()
	boot.csrf_token = csrf_token
	return boot


def get_boot():
	return jingrow._dict(
		jingrow_version=jingrow.__version__,
		jcloud_frontend_sentry_dsn=jingrow.conf.jcloud_frontend_sentry_dsn or "",
		jcloud_dashboard_sentry_dsn=jingrow.conf.jcloud_dashboard_sentry_dsn or "",
		jcloud_frontend_posthog_host=jingrow.conf.posthog_host or "",
		jcloud_frontend_posthog_project_id=jingrow.conf.posthog_project_id or "",
		jcloud_site_name=jingrow.conf.site,
		site_name=jingrow.local.site,
		default_team=get_default_team_for_user(jingrow.session.user),
		valid_teams=get_valid_teams_for_user(jingrow.session.user),
		is_system_user=jingrow.session.data.user_type == "System User",
		verify_cards_with_micro_charge=jingrow.db.get_single_value(
			"Jcloud Settings", "verify_cards_with_micro_charge"
		),
		**(
			jingrow.db.get_values(
				"Jcloud Settings",
				"Jcloud Settings",
				["free_credits_cny", "free_credits_usd"],
				as_dict=True,
			)[0]
		),
	)
