# Copyright (c) 2019, JINGROW and Contributors
# License: MIT. See LICENSE
import json

import jingrow
from jingrow.contacts.pagetype.contact.contact import get_contact_name
from jingrow.core.pagetype.user.user import create_contact
from jingrow.tests.utils import JingrowTestCase
from jingrow.website.pagetype.personal_data_download_request.personal_data_download_request import (
	get_user_data,
)


class TestRequestPersonalData(JingrowTestCase):
	def setUp(self):
		create_user_if_not_exists(email="test_privacy@example.com")

	def tearDown(self):
		jingrow.db.delete("Personal Data Download Request")

	def test_user_data_creation(self):
		user_data = json.loads(get_user_data("test_privacy@example.com"))
		contact_name = get_contact_name("test_privacy@example.com")
		expected_data = {"Contact": jingrow.get_all("Contact", {"name": contact_name}, ["*"])}
		expected_data = json.loads(json.dumps(expected_data, default=str))
		self.assertEqual({"Contact": user_data["Contact"]}, expected_data)

	def test_file_and_email_creation(self):
		jingrow.set_user("test_privacy@example.com")
		download_request = jingrow.get_pg(
			{"pagetype": "Personal Data Download Request", "user": "test_privacy@example.com"}
		)
		download_request.save(ignore_permissions=True)

		jingrow.set_user("Administrator")

		file_count = jingrow.db.count(
			"File",
			{
				"attached_to_pagetype": "Personal Data Download Request",
				"attached_to_name": download_request.name,
			},
		)

		self.assertEqual(file_count, 1)

		email_queue = jingrow.get_all("Email Queue", fields=["message"], order_by="creation DESC", limit=1)
		self.assertIn(jingrow._("Download Your Data"), email_queue[0].message)

		jingrow.db.delete("Email Queue")


def create_user_if_not_exists(email, first_name=None):
	jingrow.delete_pg_if_exists("User", email)

	user = jingrow.get_pg(
		{
			"pagetype": "User",
			"user_type": "Website User",
			"email": email,
			"send_welcome_email": 0,
			"first_name": first_name or email.split("@", 1)[0],
			"birth_date": jingrow.utils.now_datetime(),
		}
	).insert(ignore_permissions=True)
	create_contact(user=user)
