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


import jingrow
from jingrow.utils import update_progress_bar


def execute():
	jingrow.reload_pg("jcloud", "pagetype", "team")
	jingrow.reload_pg("jcloud", "pagetype", "team_onboarding")

	teams = jingrow.db.get_all("Team")
	for i, team in enumerate(teams):
		update_progress_bar("Updating onboarding", i, len(teams))

		pg = jingrow.get_pg("Team", team)

		if pg.onboarding:
			continue

		pg.initialize_onboarding_steps()

		if pg.jerp_partner:
			update_onboarding(pg, "Add Billing Information", "Skipped")
			update_onboarding(pg, "Transfer Credits", "Skipped")
			update_onboarding(pg, "Create Site", "Skipped")

		if pg.default_payment_method:
			update_onboarding(pg, "Add Billing Information", "Completed")

		if jingrow.db.count("Site", {"team": pg.name}) > 0:
			update_onboarding(pg, "Create Site", "Completed")

		pg.save()

	print()


def update_onboarding(team, step_name, status):
	for step in team.onboarding:
		if step.step_name == step_name:
			step.status = status
