import frappe
from frappe import _


class PaymentRequestMixin:
	"""Workaround for wkhtmltopdf network timeout on Payment Request submit.

	wkhtmltopdf fails with OSError when it cannot reach external resources
	(fonts, CSS) during PDF generation for the email attachment. The submit
	itself is valid — we catch the error, log it, warn the user, and proceed.
	"""

	def before_submit(self):
		try:
			super().before_submit()
		except OSError as e:
			if "wkhtmltopdf" in str(e) or "pdf" in str(e).lower():
				frappe.log_error(str(e), "Payment Request PDF Generation Failed")
				frappe.msgprint(
					_("Warning: payment request email could not be sent (PDF generation failed). Proceeding with submit."),
					alert=True,
					indicator="orange",
				)
			else:
				raise
