# Copyright (c) 2023, JINGROW
# For license information, please see license.txt

from __future__ import annotations

from typing import TYPE_CHECKING

import jingrow
from jingrow.model.page import Page

from jcloud.utils import get_current_team

if TYPE_CHECKING:
	from jcloud.jcloud.pagetype.bench.bench import Bench


class BenchUpdate(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.bench_site_update.bench_site_update import BenchSiteUpdate
		from jcloud.jcloud.pagetype.bench_update_app.bench_update_app import BenchUpdateApp
		from jcloud.jcloud.pagetype.release_group.release_group import ReleaseGroup

		apps: DF.Table[BenchUpdateApp]
		bench: DF.Link | None
		candidate: DF.Link | None
		group: DF.Link
		is_inplace_update: DF.Check
		sites: DF.Table[BenchSiteUpdate]
	# end: auto-generated types

	def validate(self):
		if not self.is_new():
			return

		self.validate_pending_updates()
		self.validate_pending_site_updates()

		if self.is_inplace_update:
			self.validate_inplace_update()

	def before_save(self):
		if not self.is_inplace_update:
			return

		site = self.sites[0].site  # validation should throw if no sites
		self.bench = jingrow.get_value("Site", site, "bench")

	def validate_pending_updates(self):
		if jingrow.get_pg("Release Group", self.group).deploy_in_progress:
			jingrow.throw("A deploy for this bench is already in progress", jingrow.ValidationError)

	def validate_pending_site_updates(self):
		for site in self.sites:
			if jingrow.db.exists(
				"Site Update",
				{"site": site.site, "status": ("in", ("Pending", "Running"))},
			):
				jingrow.throw("An update is already pending for this site", jingrow.ValidationError)

	def validate_inplace_update(self):
		sites = [s.site for s in self.sites if s.site]
		if len(sites) == 0:
			jingrow.throw(
				"In place update cannot be run without a site being selected",
			)

		benches = jingrow.get_all(
			"Site",
			fields=["bench"],
			filters={"name": ["in", sites]},
			pluck="bench",
		)

		if len(set(benches)) > 1:
			jingrow.throw(
				"In place update can be used only to update single benches",
				jingrow.ValidationError,
			)

	def deploy(self, run_will_fail_check=False) -> str:
		rg: ReleaseGroup = jingrow.get_pg("Release Group", self.group)
		candidate = rg.create_deploy_candidate(self.apps, run_will_fail_check)
		candidate.schedule_build_and_deploy()

		self.candidate = candidate.name
		self.save()

		if not isinstance(candidate.name, str):
			raise Exception(
				f"Invalid name found for deploy candidate '{candidate.name}' of type {type(candidate.name)}"
			)

		return candidate.name

	def update_inplace(self) -> str:
		if not self.is_inplace_update:
			raise Exception("In place update flag is not set, aborting in place update.")

		bench: "Bench" = jingrow.get_pg("Bench", self.bench)
		sites = [s.site for s in self.sites]

		return bench.update_inplace(self.apps, sites)

	def update_sites_on_server(self, bench, server):
		# This method gets called multiple times concurrently when a new candidate is deployed
		# Avoid saving the pg to avoid TimestampMismatchError
		if jingrow.get_value("Bench", bench, "status") != "Active":
			return

		for row in self.sites:
			if row.server != server:
				continue

			# Don't try to update if the site is already on another bench
			# It already could be on newest bench and Site Update couldn't be scheduled
			# In any case our job was to move site to a newer than this, which is already done
			current_site_bench = jingrow.get_value("Site", row.site, "bench")
			if row.source_candidate != jingrow.get_value("Bench", current_site_bench, "candidate"):
				jingrow.db.set_value("Bench Site Update", row.name, "status", "Success")
				jingrow.db.commit()
				continue

			if row.status == "Pending" and not row.site_update:
				try:
					if jingrow.get_all(
						"Site Update",
						{"site": row.site, "status": ("in", ("Pending", "Running", "Failure"))},
						ignore_ifnull=True,
						limit=1,
					):
						continue
					site_update = jingrow.get_pg("Site", row.site).schedule_update(
						skip_failing_patches=row.skip_failing_patches, skip_backups=row.skip_backups
					)
					jingrow.db.set_value("Bench Site Update", row.name, "site_update", site_update)
					jingrow.db.commit()
				except Exception:
					# Rollback the failed attempt and set status to Failure
					# So, we don't try again
					# TODO: Add Notifications
					jingrow.db.rollback()
					jingrow.db.set_value("Bench Site Update", row.name, "status", "Failure")
					traceback = jingrow.get_traceback(with_context=True)
					comment = f"Failed to schedule update for {row.site} <br><br><pre><code>{traceback}</pre></code>"
					self.add_comment(text=comment)
					jingrow.db.commit()


def get_bench_update(
	name: str,
	apps: list,
	sites: str | list[str] | None = None,
	is_inplace_update: bool = False,
) -> BenchUpdate:
	if sites is None:
		sites = []

	current_team = get_current_team()
	rg_team = jingrow.db.get_value("Release Group", name, "team")

	if rg_team != current_team:
		jingrow.throw("Bench can only be deployed by the bench owner", exc=jingrow.PermissionError)

	bench_update: "BenchUpdate" = jingrow.get_pg(
		{
			"pagetype": "Bench Update",
			"group": name,
			"apps": apps,
			"sites": [
				{
					"site": site["name"],
					"server": site["server"],
					"skip_failing_patches": site["skip_failing_patches"],
					"skip_backups": site["skip_backups"],
					"source_candidate": jingrow.get_value("Bench", site["bench"], "candidate"),
				}
				for site in sites
			],
			"is_inplace_update": is_inplace_update,
		}
	).insert(ignore_permissions=True)
	return bench_update
