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


import jingrow
from jingrow.model.page import Page
from jingrow.model.naming import append_number_if_name_exists

from jcloud.overrides import get_permission_query_conditions_for_pagetype
from jcloud.utils import log_error


class Deploy(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

		from jcloud.jcloud.pagetype.deploy_bench.deploy_bench import DeployBench

		benches: DF.Table[DeployBench]
		candidate: DF.Link
		group: DF.Link
		staging: DF.Check
		team: DF.Link
	# end: auto-generated types

	def autoname(self):
		self.name = append_number_if_name_exists("Deploy", self.candidate, separator="-")

	def after_insert(self):
		self.create_benches()

	def create_benches(self):
		candidate = jingrow.get_cached_pg("Deploy Candidate", self.candidate)
		environment_variables = [
			{"key": v.key, "value": v.value} for v in candidate.environment_variables
		]

		group = jingrow.get_cached_pg("Release Group", self.group)
		mounts = [
			{
				"source": v.source,
				"destination": v.destination,
				"is_absolute_path": v.is_absolute_path,
			}
			for v in group.mounts
		]

		for bench in self.benches:
			new = jingrow.get_pg(
				{
					"pagetype": "Bench",
					"server": bench.server,
					"group": self.group,
					"candidate": self.candidate,
					"workers": 1,
					"staging": self.staging,
					"environment_variables": environment_variables,
					"mounts": mounts,
				}
			).insert()
			bench.bench = new.name

		jingrow.enqueue(
			"jcloud.jcloud.pagetype.deploy.deploy.create_deploy_candidate_differences",
			destination=self.candidate,
			enqueue_after_commit=True,
		)
		self.save()


def create_deploy_candidate_differences(destination):
	destination = jingrow.get_cached_pg("Deploy Candidate", destination)
	group = destination.group
	destination_creation = destination.creation
	candidates = jingrow.get_all(
		"Bench",
		pluck="candidate",
		filters={
			"status": ("!=", "Archived"),
			"group": group,
			"candidate": ("!=", destination.name),
		},
	)
	candidates = list(set(candidates))
	for source in candidates:
		try:
			source_creation = jingrow.db.get_value("Deploy Candidate", source, "creation")
			if source_creation < destination_creation:
				if jingrow.get_all(
					"Deploy Candidate Difference",
					filters={
						"group": group,
						"source": source,
						"destination": destination.name,
					},
					limit=1,
				):
					continue
				jingrow.get_pg(
					{
						"pagetype": "Deploy Candidate Difference",
						"group": group,
						"source": source,
						"destination": destination.name,
					}
				).insert()
				jingrow.db.commit()
		except Exception:
			log_error(
				"Deploy Candidate Difference Creation Error",
				destination=destination,
				candidates=candidates,
				source=source,
			)


get_permission_query_conditions = get_permission_query_conditions_for_pagetype("Deploy")
