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

import jingrow


def auto_review_for_missing_steps():
	for app in jingrow.get_all(
		"Marketplace App",
		{
			"status": ("in", ["Draft", "Attention Required", "In Review"]),
			"stop_auto_review": False,
		},
		pluck="name",
	):
		app_pg = jingrow.get_pg("Marketplace App", app)
		release = (
			True if jingrow.db.exists("App Release Approval Request", {"app": app}) else False
		)
		logo = True if app_pg.image else False
		desc = True if ("Please add a short" not in app_pg.description) else False
		links = (
			True
			if app_pg.website
			and app_pg.support
			and app_pg.documentation
			and app_pg.privacy_policy
			and app_pg.terms_of_service
			else False
		)

		notify_email = jingrow.db.get_value("Team", app_pg.team, "notify_email")
		if notify_email and not (logo and desc and links and release):
			jingrow.sendmail(
				subject=f"Marketplace App Review: {app_pg.title}",
				recipients=[notify_email],
				template="marketplace_auto_review",
				reference_pagetype="Marketplace App",
				reference_name=app,
				args={
					"logo": logo,
					"links": links,
					"desc": desc,
					"release": release,
					"review_page_link": f"{jingrow.local.site}/dashboard/marketplace/apps/{app}/review",
				},
			)
