# Copyright (c) 2022, Xhive LLC
# License: GNU General Public License v3. See license.txt

import json

import xhiveframework
from xhiveframework import _

from xhiveerp.accounts.doctype.account.account import get_account_currency


def make_company_fixtures(doc, method=None):
	if not xhiveframework.flags.country_change:
		return

	run_regional_setup(doc.country)
	make_salary_components(doc.country)


def delete_company_fixtures():
	countries = xhiveframework.get_all(
		"Company",
		distinct="True",
		pluck="country",
	)

	for country in countries:
		try:
			module_name = f"hrms.regional.{xhiveframework.scrub(country)}.setup.uninstall"
			xhiveframework.get_attr(module_name)()
		except ImportError:
			pass
		except Exception:
			xhiveframework.log_error("Unable to delete country fixtures for HRMS")
			xhiveframework.throw(
				_("Failed to delete defaults for country {0}. Please contact support.").format(
					xhiveframework.bold(country)
				)
			)


def run_regional_setup(country):
	try:
		module_name = f"hrms.regional.{xhiveframework.scrub(country)}.setup.setup"
		xhiveframework.get_attr(module_name)()
	except ImportError:
		pass
	except Exception:
		xhiveframework.log_error("Unable to setup country fixtures for HRMS")
		xhiveframework.throw(
			_("Failed to setup defaults for country {0}. Please contact support.").format(
				xhiveframework.bold(country)
			)
		)


def make_salary_components(country):
	docs = []

	file_name = "salary_components.json"

	# default components already added
	if not xhiveframework.db.exists("Salary Component", "Basic"):
		file_path = xhiveframework.get_app_path("hrms", "payroll", "data", file_name)
		docs.extend(json.loads(read_data_file(file_path)))

	file_path = xhiveframework.get_app_path("hrms", "regional", xhiveframework.scrub(country), "data", file_name)
	docs.extend(json.loads(read_data_file(file_path)))

	for d in docs:
		try:
			doc = xhiveframework.get_doc(d)
			doc.flags.ignore_permissions = True
			doc.flags.ignore_mandatory = True
			doc.insert(ignore_if_duplicate=True)
		except xhiveframework.NameError:
			xhiveframework.clear_messages()
		except xhiveframework.DuplicateEntryError:
			xhiveframework.clear_messages()


def read_data_file(file_path):
	try:
		with open(file_path, "r") as f:
			return f.read()
	except IOError:
		return "{}"


def set_default_hr_accounts(doc, method=None):
	if xhiveframework.local.flags.ignore_chart_of_accounts:
		return

	if not doc.default_payroll_payable_account:
		payroll_payable_account = xhiveframework.db.get_value(
			"Account", {"account_name": _("Payroll Payable"), "company": doc.name, "is_group": 0}
		)

		doc.db_set("default_payroll_payable_account", payroll_payable_account)

	if not doc.default_employee_advance_account:
		employe_advance_account = xhiveframework.db.get_value(
			"Account", {"account_name": _("Employee Advances"), "company": doc.name, "is_group": 0}
		)

		doc.db_set("default_employee_advance_account", employe_advance_account)


def validate_default_accounts(doc, method=None):
	if doc.default_payroll_payable_account:
		for_company = xhiveframework.db.get_value("Account", doc.default_payroll_payable_account, "company")
		if for_company != doc.name:
			xhiveframework.throw(
				_("Account {0} does not belong to company: {1}").format(
					doc.default_payroll_payable_account, doc.name
				)
			)

		if get_account_currency(doc.default_payroll_payable_account) != doc.default_currency:
			xhiveframework.throw(
				_(
					"{0} currency must be same as company's default currency. Please select another account."
				).format(xhiveframework.bold("Default Payroll Payable Account"))
			)
