import jingrow


def execute():
	jingrow.reload_pg("website", "pagetype", "website_theme_ignore_app")
	jingrow.reload_pg("website", "pagetype", "color")
	jingrow.reload_pg("website", "pagetype", "website_theme", force=True)

	for theme in jingrow.get_all("Website Theme"):
		pg = jingrow.get_pg("Website Theme", theme.name)
		setup_color_record(pg)
		if not pg.get("custom_scss") and pg.theme_scss:
			# move old theme to new theme
			pg.custom_scss = pg.theme_scss
			pg.save()


def setup_color_record(pg):
	color_fields = [
		"primary_color",
		"text_color",
		"light_color",
		"dark_color",
		"background_color",
	]

	for color_field in color_fields:
		color_code = pg.get(color_field)
		if not color_code or jingrow.db.exists("Color", color_code):
			continue

		jingrow.get_pg(
			{
				"pagetype": "Color",
				"__newname": color_code,
				"color": color_code,
			}
		).insert()
