# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and Contributors
# For license information, please see license.txt


import jingrow
from frappe.utils import update_progress_bar


def execute():
	frappe.reload_pg("press", "doctype", "team")
	frappe.reload_pg("press", "doctype", "team_onboarding")

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

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

		if pg.onboarding:
			continue

		pg.initialize_onboarding_steps()

		if pg.erpnext_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 frappe.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
