# Copyright (c) 2021, XhiveFramework Technologies and contributors
# License: MIT. See LICENSE

import json

import xhiveframework
from xhiveframework import _
from xhiveframework.model.document import Document
from xhiveframework.modules.export_file import export_to_files


class FormTour(Document):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from xhiveframework.desk.doctype.form_tour_step.form_tour_step import FormTourStep
		from xhiveframework.types import DF

		dashboard_name: DF.Link | None
		first_document: DF.Check
		include_name_field: DF.Check
		is_standard: DF.Check
		list_name: DF.Literal[
			"List", "Report", "Dashboard", "Kanban", "Gantt", "Calendar", "File", "Image", "Inbox", "Map"
		]
		module: DF.Link | None
		new_document_form: DF.Check
		page_name: DF.Link | None
		page_route: DF.SmallText | None
		reference_doctype: DF.Link | None
		report_name: DF.Link | None
		save_on_complete: DF.Check
		steps: DF.Table[FormTourStep]
		title: DF.Data
		track_steps: DF.Check
		ui_tour: DF.Check
		view_name: DF.Literal["Workspaces", "List", "Form", "Tree", "Page"]
		workspace_name: DF.Link | None

	# end: auto-generated types
	def before_save(self):
		if self.is_standard and not self.module:
			if self.workspace_name:
				self.module = xhiveframework.db.get_value("Workspace", self.workspace_name, "module")
			elif self.dashboard_name:
				dashboard_doctype = xhiveframework.db.get_value("Dashboard", self.dashboard_name, "module")
				self.module = xhiveframework.db.get_value("DocType", dashboard_doctype, "module")
			else:
				self.module = "Desk"
		if not self.ui_tour:
			meta = xhiveframework.get_meta(self.reference_doctype)
			for step in self.steps:
				if step.is_table_field and step.parent_fieldname:
					parent_field_df = meta.get_field(step.parent_fieldname)
					step.child_doctype = parent_field_df.options
					field_df = xhiveframework.get_meta(step.child_doctype).get_field(step.fieldname)
					step.label = field_df.label
					step.fieldtype = field_df.fieldtype
				else:
					field_df = meta.get_field(step.fieldname)
					step.label = field_df.label
					step.fieldtype = field_df.fieldtype

	def on_update(self):
		xhiveframework.cache.delete_key("bootinfo")

		if xhiveframework.conf.developer_mode and self.is_standard:
			export_to_files([["Form Tour", self.name]], self.module)

	def on_trash(self):
		xhiveframework.cache.delete_key("bootinfo")


@xhiveframework.whitelist()
def reset_tour(tour_name):
	for user in xhiveframework.get_all("User", pluck="name"):
		onboarding_status = xhiveframework.parse_json(xhiveframework.db.get_value("User", user, "onboarding_status"))
		onboarding_status.pop(tour_name, None)
		xhiveframework.db.set_value(
			"User", user, "onboarding_status", xhiveframework.as_json(onboarding_status), update_modified=False
		)
		xhiveframework.cache.hdel("bootinfo", user)

	xhiveframework.msgprint(_("Successfully reset onboarding status for all users."), alert=True)


@xhiveframework.whitelist()
def update_user_status(value, step):
	from xhiveframework.utils.telemetry import capture

	step = xhiveframework.parse_json(step)
	tour = xhiveframework.parse_json(value)

	capture(
		xhiveframework.scrub(f"{step.parent}_{step.title}"),
		app="xhiveframework_ui_tours",
		properties={"is_completed": tour.is_completed},
	)
	xhiveframework.db.set_value("User", xhiveframework.session.user, "onboarding_status", value, update_modified=False)

	xhiveframework.cache.hdel("bootinfo", xhiveframework.session.user)


def get_onboarding_ui_tours():
	if not xhiveframework.get_system_settings("enable_onboarding"):
		return []

	ui_tours = xhiveframework.get_all("Form Tour", filters={"ui_tour": 1}, fields=["page_route", "name"])

	return [[tour.name, json.loads(tour.page_route)] for tour in ui_tours]
