import os

import xhiveframework
from xhiveframework import _


def execute():
	xhiveframework.reload_doc("email", "doctype", "email_template")
	xhiveframework.reload_doc("stock", "doctype", "delivery_settings")

	if not xhiveframework.db.exists("Email Template", _("Dispatch Notification")):
		base_path = xhiveframework.get_app_path("xhiveerp", "stock", "doctype")
		response = xhiveframework.read_file(
			os.path.join(base_path, "delivery_trip/dispatch_notification_template.html")
		)

		xhiveframework.get_doc(
			{
				"doctype": "Email Template",
				"name": _("Dispatch Notification"),
				"response": response,
				"subject": _("Your order is out for delivery!"),
				"owner": xhiveframework.session.user,
			}
		).insert(ignore_permissions=True)

	delivery_settings = xhiveframework.get_doc("Delivery Settings")
	delivery_settings.dispatch_template = _("Dispatch Notification")
	delivery_settings.flags.ignore_links = True
	delivery_settings.save()
