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


import jingrow
from jingrow.utils.fixtures import sync_fixtures


def execute():
	jingrow.reload_pg("jcloud", "pagetype", "app")
	jingrow.reload_pg("jcloud", "pagetype", "app_source")
	jingrow.reload_pg("jcloud", "pagetype", "jingrow_version")
	sync_fixtures("jcloud")
	jingrow.reload_pg("jcloud", "pagetype", "app_source_version")
	jingrow.reload_pg("jcloud", "pagetype", "app_release")
	jingrow.reload_pg("jcloud", "pagetype", "app_release_difference")
	jingrow.reload_pg("jcloud", "pagetype", "release_group_app")
	jingrow.reload_pg("jcloud", "pagetype", "bench_app")
	jingrow.reload_pg("jcloud", "pagetype", "deploy_candidate_app")
	distinct_apps = jingrow.get_all("App", ["title", "scrubbed"], group_by="scrubbed")

	for distinct_app in distinct_apps:
		apps = jingrow.get_all(
			"App", "*", {"scrubbed": distinct_app.scrubbed}, order_by="enabled desc"
		)
		for app in apps:
			versions = set(jingrow.get_all("Release Group", {"app": app.name}, pluck="version"))
			if not versions:
				groups = jingrow.get_all("Bench", {"app": app.name}, pluck="group")
				versions = set(
					jingrow.get_all("Release Group", {"name": ("in", groups)}, pluck="version")
				)
			if not versions:
				continue
			source = {
				"pagetype": "App Source",
				"app": app.name,
				"app_title": app.title,
				"jingrow": app.jingrow,
				"enabled": app.enabled,
				"repository_url": app.url,
				"repository": app.repo,
				"repository_owner": app.repo_owner,
				"branch": app.branch,
				"github_installation_id": app.installation,
				"public": app.public,
				"team": app.team,
				"versions": [{"version": version} for version in versions],
			}
			source = jingrow.get_pg(source)
			source.name = "TEMP-SOURCE"
			source.set_parent_in_children()
			source.db_insert()

			for child in source.get_all_children():
				child.db_insert()

			jingrow.db.set_value("App Release", {"app": app.name}, "source", source.name)
			jingrow.db.set_value("Bench App", {"app": app.name}, "source", source.name)
			jingrow.db.set_value("Deploy Candidate App", {"app": app.name}, "source", source.name)
			jingrow.db.set_value("Release Group App", {"app": app.name}, "source", source.name)
			jingrow.db.set_value("Release Group App", {"app": app.name}, "title", app.title)

			existing = jingrow.db.exists("App", app.scrubbed, cache=False)
			if existing and existing == app.scrubbed:
				jingrow.rename_pg("App", app.name, app.scrubbed, merge=True)
			else:
				jingrow.rename_pg("App", app.name, app.scrubbed)

			old_source_name = source.name
			source.reload()
			source.autoname()
			jingrow.rename_pg("App Source", old_source_name, source.name)


def delete():
	jingrow.db.set_value("App Release", {"cloned": False}, "source", None)
	for difference in jingrow.get_all("App Release Difference"):
		jingrow.delete_pg("App Release Difference", difference.name)
	for source in jingrow.get_all("App Source"):
		jingrow.delete_pg("App Source", source.name)
	jingrow.db.delete(
		"Patch Log", {"patch": "jcloud.patches.v0_0_1.create_app_source_from_app"}
	)
