# Copyright (c) 2021, Xhive LLC
# See license.txt

import os
import unittest

import xhiveframework
from xhiveframework import _
from xhiveframework.core.doctype.user_permission.test_user_permission import create_user
from xhiveframework.tests.test_webform import create_custom_doctype, create_webform
from xhiveframework.utils import getdate

from xhiveerp.setup.doctype.employee.test_employee import make_employee

from hrms.hr.doctype.exit_interview.exit_interview import send_exit_questionnaire


class TestExitInterview(unittest.TestCase):
	def setUp(self):
		xhiveframework.db.sql("delete from `tabExit Interview`")

	def test_duplicate_interview(self):
		employee = make_employee("employeeexitint1@example.com")
		xhiveframework.db.set_value("Employee", employee, "relieving_date", getdate())
		interview = create_exit_interview(employee)

		doc = xhiveframework.copy_doc(interview)
		self.assertRaises(xhiveframework.DuplicateEntryError, doc.save)

	def test_relieving_date_validation(self):
		employee = make_employee("employeeexitint2@example.com")
		# unset relieving date
		xhiveframework.db.set_value("Employee", employee, "relieving_date", None)

		interview = create_exit_interview(employee, save=False)
		self.assertRaises(xhiveframework.ValidationError, interview.save)

		# set relieving date
		xhiveframework.db.set_value("Employee", employee, "relieving_date", getdate())
		interview = create_exit_interview(employee)
		self.assertTrue(interview.name)

	def test_interview_date_updated_in_employee_master(self):
		employee = make_employee("employeeexit3@example.com")
		xhiveframework.db.set_value("Employee", employee, "relieving_date", getdate())

		interview = create_exit_interview(employee)
		interview.status = "Completed"
		interview.employee_status = "Exit Confirmed"

		# exit interview date updated on submit
		interview.submit()
		self.assertEqual(xhiveframework.db.get_value("Employee", employee, "held_on"), interview.date)

		# exit interview reset on cancel
		interview.reload()
		interview.cancel()
		self.assertEqual(xhiveframework.db.get_value("Employee", employee, "held_on"), None)

	def test_send_exit_questionnaire(self):
		create_custom_doctype()
		create_webform()
		template = create_notification_template()

		webform = xhiveframework.db.get_all("Web Form", limit=1)
		xhiveframework.db.set_value(
			"HR Settings",
			"HR Settings",
			{
				"exit_questionnaire_web_form": webform[0].name,
				"exit_questionnaire_notification_template": template,
			},
		)

		employee = make_employee("employeeexit3@example.com")
		xhiveframework.db.set_value("Employee", employee, "relieving_date", getdate())

		interview = create_exit_interview(employee)
		send_exit_questionnaire([interview])

		email_queue = xhiveframework.db.get_all("Email Queue", ["name", "message"], limit=1)
		self.assertTrue("Subject: Exit Questionnaire Notification" in email_queue[0].message)

	def tearDown(self):
		xhiveframework.db.rollback()


def create_exit_interview(employee, save=True):
	interviewer = create_user("test_exit_interviewer@example.com")

	doc = xhiveframework.get_doc(
		{
			"doctype": "Exit Interview",
			"employee": employee,
			"company": "_Test Company",
			"status": "Pending",
			"date": getdate(),
			"interviewers": [{"interviewer": interviewer.name}],
			"interview_summary": "Test",
		}
	)

	if save:
		return doc.insert()
	return doc


def create_notification_template():
	template = xhiveframework.db.exists("Email Template", _("Exit Questionnaire Notification"))
	if not template:
		base_path = xhiveframework.get_app_path("xhiveerp", "hr", "doctype")
		response = xhiveframework.read_file(
			os.path.join(base_path, "exit_interview/exit_questionnaire_notification_template.html")
		)

		template = xhiveframework.get_doc(
			{
				"doctype": "Email Template",
				"name": _("Exit Questionnaire Notification"),
				"response": response,
				"subject": _("Exit Questionnaire Notification"),
				"owner": xhiveframework.session.user,
			}
		).insert(ignore_permissions=True)
		template = template.name

	return template
