<?php
/**
 * Email HTML Template
 *
 * Renders a simple, battle-tested HTML email wrapper compatible with all major
 * email clients (Gmail, Outlook, Apple Mail, mobile clients).
 *
 * The outer table-based layout follows the industry-standard hybrid approach:
 * - Uses HTML4/XHTML-era table layout for Outlook (no CSS grid/flexbox support).
 * - Inline CSS only — no <style> blocks, no external stylesheets.
 * - Max-width 600px — the universally safe email width.
 * - System-font stack for reliable rendering everywhere.
 *
 * Customisation (low to high specificity):
 *  1. Filter `ambh_email_template_vars` to change colours, site name, footer text.
 *  2. Filter `ambh_email_template_html` to replace the entire template.
 *
 * @package AmbassadorHub\Mail
 */

namespace AmbassadorHub\Mail;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Generates the HTML wrapper for transactional emails.
 */
final class EmailTemplate {

	/**
	 * Wrap plain HTML body content in the full email shell.
	 *
	 * @param string $body_html    Pre-rendered HTML for the main content area.
	 *                             Paragraphs, links, and simple lists are safe.
	 * @param string $preview_text Short preview/preheader text shown in inbox
	 *                             previews (50–90 chars recommended, optional).
	 * @return string Full HTML email document.
	 */
	public static function wrap( string $body_html, string $preview_text = '' ): string {

		/**
		 * Filter template variables used to render the email wrapper.
		 *
		 * @param array $vars {
		 *   @type string $site_name      Site display name. Default: get_bloginfo('name').
		 *   @type string $site_url       Site URL used for the logo link.
		 *   @type string $color_primary  Brand accent colour (hex). Default '#0073aa'.
		 *   @type string $color_bg       Page background colour. Default '#f0f0f1'.
		 *   @type string $color_card     Card/content area background. Default '#ffffff'.
		 *   @type string $color_text     Body text colour. Default '#3c434a'.
		 *   @type string $color_muted    Muted/footer text colour. Default '#787c82'.
		 *   @type string $footer_text    Footer disclaimer line (plain text).
		 * }
		 */
		$vars = apply_filters(
			'ambh_email_template_vars',
			array(
				'site_name'     => get_bloginfo( 'name' ),
				'site_url'      => home_url( '/' ),
				'color_primary' => '#0073aa',
				'color_bg'      => '#f0f0f1',
				'color_card'    => '#ffffff',
				'color_text'    => '#3c434a',
				'color_muted'   => '#787c82',
				'footer_text'   => sprintf(
					/* translators: %s: site name */
					esc_html__( 'You received this email because of your activity on %s.', 'ambassadorhub' ),
					get_bloginfo( 'name' )
				),
			)
		);

		$site_name     = esc_html( $vars['site_name'] );
		$site_url      = esc_url( $vars['site_url'] );
		$color_primary = esc_attr( $vars['color_primary'] );
		$color_bg      = esc_attr( $vars['color_bg'] );
		$color_card    = esc_attr( $vars['color_card'] );
		$color_text    = esc_attr( $vars['color_text'] );
		$color_muted   = esc_attr( $vars['color_muted'] );
		$footer_text   = esc_html( $vars['footer_text'] );

		// Preheader: invisible text that appears as inbox preview snippet.
		// The &zwnj; padding pushes out any content that might bleed in.
		$preheader_html = '';
		if ( ! empty( $preview_text ) ) {
			$padded         = esc_html( $preview_text )
				. str_repeat( '&nbsp;&zwnj;', 80 );
			$preheader_html = '<div style="display:none;font-size:1px;color:' . $color_bg . ';line-height:1px;max-height:0;max-width:0;opacity:0;overflow:hidden;">'
				. $padded
				. '</div>';
		}

		$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n"
			. '<html xmlns="http://www.w3.org/1999/xhtml" lang="en">' . "\n"
			. '<head>' . "\n"
			. '  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n"
			. '  <meta name="viewport" content="width=device-width, initial-scale=1.0" />' . "\n"
			. '  <!--[if !mso]><!-->' . "\n"
			. '  <meta http-equiv="X-UA-Compatible" content="IE=edge" />' . "\n"
			. '  <!--<![endif]-->' . "\n"
			. '  <title>' . $site_name . '</title>' . "\n"
			. '</head>' . "\n"
			. '<body style="margin:0;padding:0;background-color:' . $color_bg . ';-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;">' . "\n"
			. "\n"
			. $preheader_html . "\n"
			. "\n"
			. '<!-- Outer wrapper -->' . "\n"
			. '<table role="presentation" border="0" cellpadding="0" cellspacing="0" width="100%"' . "\n"
			. '       style="border-collapse:collapse;background-color:' . $color_bg . ';">' . "\n"
			. '  <tr>' . "\n"
			. '    <td align="center" style="padding:32px 12px;">' . "\n"
			. "\n"
			. '      <!-- Email card (max 600px) -->' . "\n"
			. '      <table role="presentation" border="0" cellpadding="0" cellspacing="0" width="600"' . "\n"
			. '             style="border-collapse:collapse;max-width:600px;width:100%;">' . "\n"
			. "\n"
			. '        <!-- Header / Logo row -->' . "\n"
			. '        <tr>' . "\n"
			. '          <td align="center"' . "\n"
			. '              style="padding:0 0 8px 0;">' . "\n"
			. '            <a href="' . $site_url . '" target="_blank" rel="noopener noreferrer"' . "\n"
			. '               style="font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,Oxygen-Sans,Ubuntu,Cantarell,\'Helvetica Neue\',Arial,sans-serif;font-size:22px;font-weight:700;color:' . $color_primary . ';text-decoration:none;">' . "\n"
			. '              ' . $site_name . "\n"
			. '            </a>' . "\n"
			. '          </td>' . "\n"
			. '        </tr>' . "\n"
			. "\n"
			. '        <!-- Body card -->' . "\n"
			. '        <tr>' . "\n"
			. '          <td style="background-color:' . $color_card . ';border-radius:4px;padding:32px 40px;' . "\n"
			. '                     font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,Oxygen-Sans,Ubuntu,Cantarell,\'Helvetica Neue\',Arial,sans-serif;' . "\n"
			. '                     font-size:15px;line-height:1.6;color:' . $color_text . ';">' . "\n"
			. '            ' . $body_html . "\n"
			. '          </td>' . "\n"
			. '        </tr>' . "\n"
			. "\n"
			. '        <!-- Footer -->' . "\n"
			. '        <tr>' . "\n"
			. '          <td align="center"' . "\n"
			. '              style="padding:20px 0 0 0;font-family:-apple-system,BlinkMacSystemFont,\'Segoe UI\',Roboto,Oxygen-Sans,Ubuntu,Cantarell,\'Helvetica Neue\',Arial,sans-serif;' . "\n"
			. '                     font-size:12px;line-height:1.5;color:' . $color_muted . ';">' . "\n"
			. '            ' . $footer_text . "\n"
			. '          </td>' . "\n"
			. '        </tr>' . "\n"
			. "\n"
			. '      </table>' . "\n"
			. '      <!-- /Email card -->' . "\n"
			. "\n"
			. '    </td>' . "\n"
			. '  </tr>' . "\n"
			. '</table>' . "\n"
			. '<!-- /Outer wrapper -->' . "\n"
			. "\n"
			. '</body>' . "\n"
			. '</html>';

		/**
		 * Filter the final rendered HTML email template.
		 *
		 * Return a fully-formed HTML string to bypass the default template entirely.
		 *
		 * @param string $html      Rendered HTML document.
		 * @param string $body_html The original body content passed to wrap().
		 * @param array  $vars      Resolved template variables.
		 */
		return apply_filters( 'ambh_email_template_html', $html, $body_html, $vars );
	}
}
