# Copyright (c) 2019, JINGROW
# License: MIT. See LICENSE

import json

import jingrow
from jingrow import _
from jingrow.model.page import Page
from jingrow.utils.verified_command import get_signed_params


class PersonalDataDownloadRequest(Page):
	# begin: auto-generated types
	# This code is auto-generated. Do not modify anything in this block.

	from typing import TYPE_CHECKING

	if TYPE_CHECKING:
		from jingrow.types import DF

		amended_from: DF.Link | None
		user: DF.Link
		user_name: DF.Data | None

	# end: auto-generated types
	def after_insert(self):
		personal_data = get_user_data(self.user)

		jingrow.enqueue_pg(
			self.pagetype,
			self.name,
			"generate_file_and_send_mail",
			queue="short",
			personal_data=personal_data,
			now=jingrow.flags.in_test,
		)

	def generate_file_and_send_mail(self, personal_data):
		"""generate the file link for download"""
		user_name = self.user_name.replace(" ", "-")
		f = jingrow.get_pg(
			{
				"pagetype": "File",
				"file_name": "Personal-Data-" + user_name + "-" + self.name + ".json",
				"attached_to_pagetype": "Personal Data Download Request",
				"attached_to_name": self.name,
				"content": str(personal_data),
				"is_private": 1,
			}
		)
		f.save(ignore_permissions=True)

		file_link = (
			jingrow.utils.get_url("/api/action/jingrow.utils.file_manager.download_file")
			+ "?"
			+ get_signed_params({"file_url": f.file_url})
		)
		host_name = jingrow.local.site
		jingrow.sendmail(
			recipients=self.user,
			subject=_("Download Your Data"),
			template="download_data",
			args={
				"user": self.user,
				"user_name": self.user_name,
				"link": file_link,
				"host_name": host_name,
			},
			header=[_("Download Your Data"), "green"],
		)


def get_user_data(user):
	"""returns user data not linked to User pagetype"""
	hooks = jingrow.get_hooks("user_data_fields")
	data = {}
	for hook in hooks:
		d = data.get(hook.get("pagetype"), [])
		d += jingrow.get_all(hook.get("pagetype"), {hook.get("filter_by", "owner"): user}, ["*"])
		if d:
			data.update({hook.get("pagetype"): d})
	return json.dumps(data, indent=2, default=str)
