import os

import influxframework
from influxframework import _


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

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

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

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