# -*- coding: utf-8 -*-
# Copyright (c) 2021, JINGROW
# For license information, please see license.txt

import jingrow
from jingrow.core.utils import find
from jingrow.utils.verified_command import get_signed_params
from jingrow.website.pagetype.personal_data_deletion_request.personal_data_deletion_request import (
	PersonalDataDeletionRequest,
)


def handle_exception(self):
	jingrow.db.rollback()
	traceback = f"<br><br><pre><code>{jingrow.get_traceback()}</pre></code>"
	self.add_comment(text=f"Failure occurred during Data Deletion:{traceback}")
	jingrow.db.commit()


class TeamDeletionRequest(PersonalDataDeletionRequest):
	# 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
		from jingrow.website.pagetype.personal_data_deletion_step.personal_data_deletion_step import (
			PersonalDataDeletionStep,
		)

		from jcloud.jcloud.pagetype.team_member_deletion_request.team_member_deletion_request import (
			TeamMemberDeletionRequest,
		)

		data_anonymized: DF.Check
		deletion_steps: DF.Table[PersonalDataDeletionStep]
		jingrowio_data_deleted: DF.Check
		status: DF.Literal[
			"Pending Verification", "Deletion Verified", "Processing Deletion", "Deleted"
		]
		stripe_data_deleted: DF.Check
		team: DF.Link
		team_disabled: DF.Check
		users_anonymized: DF.Table[TeamMemberDeletionRequest]
	# end: auto-generated types

	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)
		self.email = jingrow.db.get_value("Team", self.team, "user")
		# turn off data deletions in partial content for the sake of sanity
		self.full_match_privacy_docs += self.partial_privacy_docs
		self.partial_privacy_docs = []

	def before_insert(self):
		self.validate_team_owner()
		self.validate_duplicate_request()

	def after_insert(self):
		self.add_deletion_steps()
		self.set_users_anonymized()

	def validate(self):
		self.validate_sites_states()
		self.finalize_pending_invoices()
		self.validate_outstanding_invoices()

	def on_update(self):
		self.finish_up()

	def handle_exc(foo):
		def after_execute(self):
			try:
				foo(self)
			except Exception:
				handle_exception(self)

		return after_execute

	@property
	def team_pg(self):
		return jingrow.get_cached_pg("Team", self.team)

	def rename_team_on_data_deletion(self):
		if (
			self.status == "Deleted"
			and self.name != self.team
			and jingrow.db.exists("Team", self.team)
		):
			jingrow.rename_pg("Team", self.team, self.name)

	def validate_team_owner(self):
		if (
			self.team_pg.user == jingrow.session.user or "System Manager" in jingrow.get_roles()
		):
			return

		jingrow.throw(
			"You need to be a Team owner to request account deletion", exc=jingrow.PermissionError
		)

	def validate_duplicate_request(self):
		if jingrow.db.exists(self.pagetype, {"team": self.team}):
			jingrow.throw(
				f"{self.pagetype} for {self.team} already exists!", exc=jingrow.DuplicateEntryError
			)

	def delete_team_data(self):
		self.db_set("status", "Processing Deletion")
		if not self.team_disabled:
			self.disable_team()
		if not self.jingrowio_data_deleted:
			self.delete_data_on_jingrowio()
		if not self.stripe_data_deleted:
			self.delete_stripe_customer()
		if (
			self.team_disabled and self.jingrowio_data_deleted and self.stripe_data_deleted
		) and not self.data_anonymized:
			self.delete_data_on_jcloud()
		self.finish_up()

	def finish_up(self):
		if (
			self.team_disabled
			and self.jingrowio_data_deleted
			and self.stripe_data_deleted
			and self.data_anonymized
		):
			self.db_set("status", "Deleted")
			self.rename_team_on_data_deletion()
			jingrow.db.commit()
			self.reload()

	def generate_url_for_confirmation(self):
		params = get_signed_params({"team": self.team})
		api = jingrow.utils.get_url("/api/action/jcloud.api.account.delete_team")
		url = f"{api}?{params}"

		if jingrow.conf.developer_mode:
			print(f"URL generated for {self.pagetype} {self.name}: {url}")

		return url

	@handle_exc
	def disable_team(self):
		team = self.team_pg
		team.enabled = False
		team.save()
		self.db_set("team_disabled", True, commit=True)
		self.reload()

	@handle_exc
	def delete_stripe_customer(self):
		from jcloud.api.billing import get_stripe

		stripe = get_stripe()
		team = self.team_pg

		try:
			stripe.Customer.delete(team.stripe_customer_id)
		except stripe.error.InvalidRequestError as e:
			if "No such customer" not in str(e):
				raise e

		team.db_set("stripe_customer_id", False)
		self.db_set("stripe_data_deleted", True, commit=True)
		self.reload()

	@handle_exc
	def delete_data_on_jingrowio(self):
		"""Anonymize data on jingrow.com"""
		from jcloud.utils.billing import get_jingrow_io_connection

		client = get_jingrow_io_connection()
		response = client.session.delete(
			f"{client.url}/api/action/delete-fc-team",
			data={"team": self.team},
			headers=client.headers,
		)
		if not response.ok:
			response.raise_for_status()

		self.db_set("jingrowio_data_deleted", True, commit=True)
		self.reload()

	def set_users_anonymized(self):
		def numerate_email(x, i):
			user_email, domain = x.split("@")
			return f"{user_email}-{i + 1}@{domain}"

		team_members = [row.user for row in self.team_pg.team_members]
		members_only_in_this_team = [
			user
			for user in team_members
			if not jingrow.db.exists(
				"Team Member", {"user": user, "parent": ("!=", self.team_pg.name)}
			)
		]

		renamed_dict = {
			x: numerate_email(self.name, i) for i, x in enumerate(members_only_in_this_team)
		}

		for now, then in renamed_dict.items():
			self.append(
				"users_anonymized",
				{"team_member": now, "anon_team_member": then, "deletion_status": "Pending"},
			)

		self.db_update()
		self.update_children()
		jingrow.db.commit()
		self.reload()

	@handle_exc
	def delete_data_on_jcloud(self):
		if not self.users_anonymized:
			self.set_users_anonymized()

		def is_deletion_pending(email):
			return find(
				self.users_anonymized,
				lambda x: x.get("team_member") == email and x.get("deletion_status") == "Pending",
			)

		for user in self.users_anonymized:
			now = user.get("team_member")
			then = user.get("anon_team_member")

			if now == then and is_deletion_pending(now):
				# user has been anonymized. set status as deleted
				pass
			elif is_deletion_pending(now):
				self._anonymize_data(now, then, commit=True)
			else:
				continue

			try:
				self.users_anonymized = filter(
					lambda x: (x.get("team_member") != now) and (x.get("anon_team_member") != then),
					self.users_anonymized,
				)
				self.append(
					"users_anonymized",
					{"team_member": then, "anon_team_member": then, "deletion_status": "Deleted"},
				)
				self.db_update()
				self.update_children()
			except Exception:
				handle_exception(self)

			jingrow.db.commit()

		self.db_set("data_anonymized", True, commit=True)
		self.reload()

	def validate_sites_states(self):
		non_archived_sites = jingrow.get_all(
			"Site", filters={"status": ("!=", "Archived"), "team": self.team}, pluck="name"
		)
		if non_archived_sites:
			jingrow.throw(
				f"Team {self.team} has {len(non_archived_sites)} sites. Drop them"
				" before you can delete your account"
			)

	def finalize_pending_invoices(self):
		pending_invoices = jingrow.get_all(
			"Invoice",
			filters={"team": self.team},
			or_filters={"pagestatus": 0, "status": "Draft"},
			pluck="name",
		)
		for invoice in pending_invoices:
			jingrow.get_pg("Invoice", invoice).finalize_invoice()

	def validate_outstanding_invoices(self):
		if self.team_pg.is_defaulter():
			jingrow.throw("You have Unpaid Invoices. Clear them to delete your account")


def process_team_deletion_requests():
	# order in desc since deleting jcloud data takes the most time
	pagetype = "Team Deletion Request"
	deletion_requests = jingrow.get_all(
		pagetype,
		filters={"status": ("in", ["Deletion Verified", "Processing Deletion"])},
		pluck="name",
		order_by="creation desc",
	)
	for name in deletion_requests:
		try:
			tdr = jingrow.get_pg(pagetype, name)
			tdr.delete_team_data()
			jingrow.db.commit()
		except Exception:
			continue
