# Copyright (c) 2021, JINGROW and Contributors
# License: MIT. See LICENSE

import jingrow
from jingrow.desk.pagetype.global_search_settings.global_search_settings import (
	update_global_search_pagetypes,
)
from jingrow.utils.dashboard import sync_dashboards


def _(x, *args, **kwargs):
	"""Redefine the translation function to return the string as is.
	We want to create english records but still mark the strings as translatable.
	The respective PageTypes have 'Translate Link Fields' enabled."""
	return x


def install():
	update_genders()
	update_salutations()
	update_global_search_pagetypes()
	setup_email_linking()
	sync_dashboards()
	add_unsubscribe()


def update_genders():
	for gender in (
		_("Male"),
		_("Female"),
		_("Other"),
		_("Transgender"),
		_("Genderqueer"),
		_("Non-Conforming"),
		_("Prefer not to say"),
	):
		pg = jingrow.new_pg("Gender")
		pg.gender = gender
		pg.insert(ignore_permissions=True, ignore_if_duplicate=True)


def update_salutations():
	for salutation in (
		_("Mr"),
		_("Ms"),
		_("Mx"),
		_("Dr"),
		_("Mrs"),
		_("Madam"),
		_("Miss"),
		_("Master"),
		_("Prof"),
	):
		pg = jingrow.new_pg("Salutation")
		pg.salutation = salutation
		pg.insert(ignore_permissions=True, ignore_if_duplicate=True)


def setup_email_linking():
	pg = jingrow.new_pg("Email Account")
	pg.email_id = "email_linking@example.com"
	pg.insert(ignore_permissions=True, ignore_if_duplicate=True)


def add_unsubscribe():
	for unsubscribe in [
		{"email": "admin@example.com", "global_unsubscribe": 1},
		{"email": "guest@example.com", "global_unsubscribe": 1},
	]:
		if not jingrow.get_all("Email Unsubscribe", filters=unsubscribe):
			pg = jingrow.new_pg("Email Unsubscribe")
			pg.update(unsubscribe)
			pg.insert(ignore_permissions=True)
