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

from random import choice
from typing import List

import jingrow

from jcloud.jcloud.audit import get_benches_in_server
from jcloud.jcloud.pagetype.server.server import Server


class BackupTest:
	def __init__(self) -> None:
		self.trial_plans = jingrow.get_all(
			"Site Plan", dict(enabled=1, is_trial_plan=1), pluck="name"
		)
		self.sites = self.get_random_sites()

	def get_random_sites(self) -> List:
		sites = []
		servers = Server.get_all_primary_prod()
		for server in servers:
			benches = self.get_benches(server)
			for bench in benches:
				site_list = jingrow.get_all(
					"Site",
					dict(status="Active", plan=("not in", self.trial_plans), bench=bench),
					pluck="name",
				)
				if not site_list:
					continue
				site = choice(site_list)
				sites.append(site)

		return sites

	def start(self):
		for site in self.sites:
			try:
				jingrow.get_pg(
					{
						"pagetype": "Backup Restoration Test",
						"date": jingrow.utils.now_datetime(),
						"site": site,
						"status": "Running",
					}
				).insert()
			except Exception:
				jingrow.log_error("Backup Restore Test insertion failed")

	def get_benches(self, server: str) -> List[str]:
		benches = get_benches_in_server(server)

		# select all benches from central benches
		# TODO: provision to run for all release groups
		groups = jingrow.get_all(
			"Release Group", dict(enabled=1, central_bench=1), pluck="name"
		)

		bench_list = []
		for group in groups:
			group_benches = jingrow.get_all(
				"Bench", dict(status="Active", group=group, server=server), pluck="name"
			)

			for bench in benches.keys():
				if bench in group_benches:
					bench_list.append(bench)

		return bench_list


def archive_backup_test_sites():
	backup_tests = jingrow.get_all(
		"Backup Restoration Test",
		dict(status=("in", ("Archive Failed", "Success"))),
		pluck="test_site",
	)
	if backup_tests:
		for test_site in backup_tests:
			site = jingrow.get_pg("Site", test_site)
			if site.status == "Active":
				site.archive(reason="Backup Restoration Test")


def run_backup_restore_test():
	backup_restoration_test = BackupTest()
	backup_restoration_test.start()
