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


import jingrow
from jingrow.core.utils import find
from jingrow.model.page import Page

from jcloud.overrides import get_permission_query_conditions_for_pagetype


class DeployCandidateDifference(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_candidate_difference_app.deploy_candidate_difference_app import (
			DeployCandidateDifferenceApp,
		)

		apps: DF.Table[DeployCandidateDifferenceApp]
		deploy_type: DF.Literal["Pull", "Migrate"]
		destination: DF.Link
		group: DF.Link
		source: DF.Link
		team: DF.Link
	# end: auto-generated types

	def validate(self):
		if self.source == self.destination:
			jingrow.throw(
				"Destination Candidate must be different from Source Candidate",
				jingrow.ValidationError,
			)

		source_creation = jingrow.db.get_value("Deploy Candidate", self.source, "creation")
		destination_creation = jingrow.db.get_value(
			"Deploy Candidate", self.destination, "creation"
		)
		if source_creation > destination_creation:
			jingrow.throw(
				"Destination Candidate must be created after Source Candidate",
				jingrow.ValidationError,
			)

		if jingrow.get_all(
			"Deploy Candidate Difference",
			filters={
				"group": self.group,
				"source": self.source,
				"destination": self.destination,
				"name": ("!=", self.name),
			},
		):
			jingrow.throw(
				"Deploy Candidate Difference already exists for Release Group: {} "
				", Source Release: {} and Destination Release: {}".format(
					self.group, self.source, self.destination
				),
				jingrow.ValidationError,
			)

		self.populate_apps_table()

	def populate_apps_table(self):
		source_candidate = jingrow.get_pg("Deploy Candidate", self.source)
		destination_candidate = jingrow.get_pg("Deploy Candidate", self.destination)
		for destination in destination_candidate.apps:
			source = find(source_candidate.apps, lambda x: x.app == destination.app)
			if not source or source.release == destination.release:
				continue
			differences = jingrow.get_all(
				"App Release Difference",
				["name"],
				{"source_release": source.release, "destination_release": destination.release},
				limit=1,
			)
			if not differences:
				difference = jingrow.get_pg(
					{
						"pagetype": "App Release Difference",
						"app": destination.app,
						"source": destination.source,
						"source_release": source.release,
						"destination_release": destination.release,
					}
				)
				difference.insert()
				difference.set_deploy_type()
			else:
				difference = jingrow.get_pg(
					"App Release Difference", differences[0].name, for_update=True
				)
				difference.reload()
				difference.set_deploy_type()
			self.append(
				"apps",
				{
					"app": destination.app,
					"destination_release": destination.release,
					"source_release": source.release,
					"difference": difference.name,
					"deploy_type": difference.deploy_type,
				},
			)

			if difference.deploy_type == "Migrate":
				self.deploy_type = "Migrate"


get_permission_query_conditions = get_permission_query_conditions_for_pagetype(
	"Deploy Candidate Difference"
)
