# Copyright (c) 2024, Frappe and contributors
# For license information, please see license.txt

import jingrow

from press.press.doctype.communication_info.communication_info import get_communication_info


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

		recipients = get_communication_info("Email", "Marketplace", "Team", app_pg.team)
		if recipients and not (logo and desc and links and release):
			frappe.sendmail(
				subject=f"Marketplace App Review: {app_pg.title}",
				recipients=recipients,
				template="marketplace_auto_review",
				reference_doctype="Marketplace App",
				reference_name=app,
				args={
					"logo": logo,
					"links": links,
					"desc": desc,
					"release": release,
					"review_page_link": f"{frappe.local.site}/dashboard/marketplace/apps/{app}/review",
				},
			)
