<?php
/**
 * Server-rendered email previews for admin designers (no Alpine required).
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Admin;

use SuiteMkt3k\Email\Email_Renderer;

defined( 'ABSPATH' ) || exit;

final class Email_Preview_Renderer {

	/**
	 * @param array<string, mixed> $data
	 */
	public static function newsletter( array $data ): void {
		$sample_name = (string) ( $data['sample_name'] ?? __( 'there', 'suitemkt3k' ) );
		$heading     = (string) ( $data['heading'] ?? '' );
		$subject     = (string) ( $data['subject'] ?? '' );
		if ( '' === $heading ) {
			$heading = $subject;
		}
		if ( '' === $heading ) {
			$heading = (string) ( $data['heading_fallback'] ?? __( 'Big news from us', 'suitemkt3k' ) );
		}

		$body = (string) ( $data['body'] ?? '' );
		if ( '' === $body ) {
			$body = (string) ( $data['body_fallback'] ?? __( 'Your email body will appear here.', 'suitemkt3k' ) );
		} else {
			$body = str_replace( '{{name}}', $sample_name, $body );
		}

		$cta_label = (string) ( $data['cta_label'] ?? '' );
		if ( '' === $cta_label ) {
			$cta_label = (string) ( $data['cta_fallback'] ?? __( 'Discover more', 'suitemkt3k' ) );
		}

		$accent            = UI::normalize_hex_public( (string) ( $data['accent'] ?? '#2AB2E5' ) );
		$header_color      = UI::normalize_hex_public( (string) ( $data['header_color'] ?? $accent ) );
		$secondary_accent  = UI::normalize_hex_public( (string) ( $data['secondary_accent'] ?? $accent ) );
		$badge_label       = (string) ( $data['badge_label'] ?? '' );
		$cta_radius        = max( 0, min( 99, (int) ( $data['cta_radius'] ?? 10 ) ) );
		$logo_url          = (string) ( $data['logo_url'] ?? '' );
		$brand             = (string) ( $data['brand'] ?? '' );
		$image_url         = (string) ( $data['image_url'] ?? '' );
		$template          = sanitize_key( (string) ( $data['template'] ?? 'classic' ) );
		$preheader         = (string) ( $data['preheader'] ?? '' );
		$footer_color      = UI::normalize_hex_public( (string) ( $data['footer_color'] ?? '#94A3B8' ) );
		$cta_url           = (string) ( $data['cta_url'] ?? '#' );
		$unsubscribe_text  = (string) ( $data['unsubscribe_text'] ?? '' );
		$unsubscribe_cta   = (string) ( $data['unsubscribe_cta'] ?? '' );
		if ( '' === $unsubscribe_cta ) {
			$unsubscribe_cta = (string) ( $data['unsubscribe_cta_fallback'] ?? __( 'Unsubscribe', 'suitemkt3k' ) );
		}

		$footer_ctx    = Email_Renderer::footer_context();
		$footer_brand  = (string) ( $data['footer_text'] ?? $footer_ctx['footer_text'] );
		$footer_legal  = (string) ( $data['address'] ?? $footer_ctx['address'] );

		$has_logo         = '' !== $logo_url;
		$is_announce      = 'announce' === $template;
		$show_badge       = '' !== $badge_label && 'classic' !== $template;
		$cta_style        = 'announce' === $template
			? 'background:transparent;color:' . $accent . ';border:2px solid ' . $accent . ';border-radius:' . $cta_radius . 'px;'
			: 'background:' . $accent . ';border-radius:' . $cta_radius . 'px;';
		?>
		<div class="wmk-designer-preview" data-wmk-email-preview="newsletter">
			<div class="wmk-card">
				<h3><?php esc_html_e( 'Preview', 'suitemkt3k' ); ?></h3>
				<div class="wmk-preview-frame">
					<div class="wmk-preview-email wmk-pe-<?php echo esc_attr( $template ); ?>" data-wmk-preview-root>
						<div class="wmk-pe-logo-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-logo-wrap>
							<img class="wmk-pe-logo" data-wmk-preview-logo src="<?php echo esc_url( $logo_url ); ?>" alt="">
						</div>
						<div class="wmk-pe-accent-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-accent-bar style="<?php echo $has_logo ? esc_attr( 'background:' . $accent . ';' ) : ''; ?>"></div>
						<div class="wmk-pe-header<?php echo $has_logo ? ' wmk-preview-hidden' : ''; ?>" data-wmk-preview-header style="<?php echo $has_logo ? '' : esc_attr( 'background:' . $header_color . ';' ); ?>">
							<strong data-wmk-preview-brand><?php echo esc_html( $brand ); ?></strong>
						</div>
						<div class="wmk-pe-badge-wrap wmk-pe-badge-wrap--announce<?php echo $show_badge && $is_announce ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-badge-wrap data-wmk-badge-placement="above-band">
							<span class="wmk-pe-badge" data-wmk-preview-badge style="<?php echo esc_attr( 'color:' . $accent . ';background:' . $accent . '22;' ); ?>"><?php echo esc_html( $badge_label ); ?></span>
						</div>
						<div class="wmk-pe-announce-band<?php echo $is_announce ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-announce-band style="<?php echo esc_attr( 'background:' . $secondary_accent . ';' ); ?>">
							<h2 class="wmk-pe-heading wmk-pe-heading-band" data-wmk-preview-heading-band><?php echo esc_html( $heading ); ?></h2>
						</div>
						<div class="wmk-pe-image-wrap<?php echo '' !== $image_url && $is_announce ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-image-wrap-announce>
							<img class="wmk-pe-image" data-wmk-preview-image-announce src="<?php echo esc_url( $image_url ); ?>" alt="">
						</div>
						<div class="wmk-pe-image-wrap<?php echo '' !== $image_url && ! $is_announce ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-image-wrap>
							<img class="wmk-pe-image" data-wmk-preview-image src="<?php echo esc_url( $image_url ); ?>" alt="">
						</div>
						<div class="wmk-pe-body">
							<div class="wmk-pe-badge-wrap<?php echo $show_badge && ! $is_announce ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-badge-wrap-inner>
								<span class="wmk-pe-badge" data-wmk-preview-badge-inner style="<?php echo esc_attr( 'color:' . $secondary_accent . ';' ); ?>"><?php echo esc_html( $badge_label ); ?></span>
							</div>
							<h2 class="wmk-pe-heading<?php echo $is_announce ? ' wmk-preview-hidden' : ''; ?>" data-wmk-preview-heading><?php echo esc_html( $heading ); ?></h2>
							<p class="wmk-pe-paragraph" data-wmk-preview-body><?php echo esc_html( $body ); ?></p>
							<div class="wmk-pe-cta-wrap">
								<a class="wmk-pe-cta" data-wmk-preview-cta href="<?php echo esc_url( $cta_url ); ?>" style="<?php echo esc_attr( $cta_style ); ?>"><?php echo esc_html( $cta_label ); ?></a>
							</div>
						</div>
						<div class="wmk-pe-footer">
							<strong class="wmk-pe-footer-brand<?php echo '' !== $footer_brand ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-brand><?php echo esc_html( $footer_brand ); ?></strong>
							<div class="wmk-pe-footer-legal<?php echo '' !== $footer_legal ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-address><?php echo esc_html( $footer_legal ); ?></div>
							<span class="wmk-pe-footer-line<?php echo '' !== $preheader ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer style="<?php echo esc_attr( 'color:' . $footer_color . ';' ); ?>"><?php echo esc_html( $preheader ); ?></span>
							<div class="wmk-pe-unsub">
								<span data-wmk-preview-unsub-text><?php echo esc_html( $unsubscribe_text ); ?></span>
								<a href="#" data-wmk-preview-unsub-cta><?php echo esc_html( $unsubscribe_cta ); ?></a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * @param array<string, mixed> $data
	 */
	public static function cart_recovery( array $data ): void {
		$sample_name = (string) ( $data['sample_name'] ?? __( 'there', 'suitemkt3k' ) );
		$heading     = (string) ( $data['heading'] ?? '' );
		$subject     = (string) ( $data['subject'] ?? '' );
		if ( '' === $heading ) {
			$heading = $subject;
		}
		if ( '' === $heading ) {
			$heading = '—';
		}

		$body = (string) ( $data['body'] ?? '' );
		if ( '' === $body ) {
			$body = (string) ( $data['body_fallback'] ?? __( 'Your email body will appear here.', 'suitemkt3k' ) );
		} else {
			$body = str_replace( '{{name}}', $sample_name, $body );
		}

		$accent                  = UI::normalize_hex_public( (string) ( $data['accent'] ?? '#2AB2E5' ) );
		$logo_url                = (string) ( $data['logo_url'] ?? '' );
		$brand                   = (string) ( $data['brand'] ?? '' );
		$preheader               = (string) ( $data['preheader'] ?? '' );
		$cta_label               = (string) ( $data['cta_label'] ?? '' );
		$discount_percent        = (string) ( $data['discount_percent'] ?? '' );
		$discount_validity_hours = (string) ( $data['discount_validity_hours'] ?? '24' );
		$attached_coupon_id      = (int) ( $data['attached_coupon_id'] ?? 0 );
		$attached_coupon_label   = (string) ( $data['attached_coupon_label'] ?? '' );
		$attached_coupon_name    = (string) ( $data['attached_coupon_name'] ?? '' );
		$validity_text           = (string) ( $data['validity_text'] ?? '' );
		$unsubscribe_text        = (string) ( $data['unsubscribe_text'] ?? '' );
		$unsubscribe_cta         = (string) ( $data['unsubscribe_cta'] ?? '' );
		if ( '' === $unsubscribe_text ) {
			$unsubscribe_text = __( 'You no longer want to receive emails like this?', 'suitemkt3k' );
		}
		if ( '' === $unsubscribe_cta ) {
			$unsubscribe_cta = __( 'Unsubscribe', 'suitemkt3k' );
		}

		$has_logo      = '' !== $logo_url;
		$has_coupon    = $attached_coupon_id > 0;
		$has_percent   = ! $has_coupon && '' !== $discount_percent;
		$validity_line = '';
		if ( ( $has_coupon || $has_percent ) && '' !== $validity_text && '' !== $discount_validity_hours ) {
			$validity_line = strtr(
				$validity_text,
				array(
					'{{hours}}'  => $discount_validity_hours,
					'{{code}}'   => $attached_coupon_name,
					'{{amount}}' => $attached_coupon_label,
				)
			);
		}

		$cta_radius              = max( 0, min( 99, (int) ( $data['cta_radius'] ?? 10 ) ) );
		$header_text             = $brand;
		$footer_ctx              = Email_Renderer::footer_context();
		$footer_brand            = (string) ( $data['footer_text'] ?? $footer_ctx['footer_text'] );
		$footer_legal            = (string) ( $data['address'] ?? $footer_ctx['address'] );
		?>
		<div class="wmk-designer-preview" data-wmk-email-preview="cart">
			<div class="wmk-card">
				<h3><?php esc_html_e( 'Preview', 'suitemkt3k' ); ?></h3>
				<div class="wmk-preview-frame">
					<div class="wmk-preview-email" data-wmk-preview-root>
						<div class="wmk-pe-logo-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-logo-wrap>
							<img class="wmk-pe-logo" data-wmk-preview-logo src="<?php echo esc_url( $logo_url ); ?>" alt="">
						</div>
						<div class="wmk-pe-accent-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-accent-bar style="<?php echo $has_logo ? esc_attr( 'background:' . $accent . ';' ) : ''; ?>"></div>
						<div class="wmk-pe-header<?php echo $has_logo ? ' wmk-preview-hidden' : ''; ?>" data-wmk-preview-header style="<?php echo $has_logo ? '' : esc_attr( 'background:' . $accent . ';' ); ?>">
							<span data-wmk-preview-header-text><?php echo esc_html( $header_text ); ?></span>
						</div>
						<div class="wmk-pe-body">
							<h2 class="wmk-pe-heading" data-wmk-preview-heading><?php echo esc_html( $heading ); ?></h2>
							<p class="wmk-pe-paragraph" data-wmk-preview-body><?php echo esc_html( $body ); ?></p>
							<p class="<?php echo $has_coupon ? '' : 'wmk-preview-hidden '; ?>" data-wmk-preview-coupon style="background:#FEF3C7;color:#92400E;padding:10px;border-radius:8px;font-weight:600;text-align:center;margin:0 auto 14px;max-width:300px;">
								<span data-wmk-preview-coupon-label><?php echo esc_html( $attached_coupon_label ); ?></span>
								<span> · </span>
								<span data-wmk-preview-coupon-name><?php echo esc_html( $attached_coupon_name ); ?></span>
							</p>
							<p class="<?php echo $has_percent ? '' : 'wmk-preview-hidden '; ?>" data-wmk-preview-percent style="background:#FEF3C7;color:#92400E;padding:10px;border-radius:8px;font-weight:600;text-align:center;margin:0 auto 14px;max-width:300px;">
								<span data-wmk-preview-percent-value><?php echo esc_html( $discount_percent ); ?></span>% <?php esc_html_e( 'Discount', 'suitemkt3k' ); ?>
							</p>
							<p class="<?php echo '' !== $validity_line ? '' : 'wmk-preview-hidden '; ?>" data-wmk-preview-validity style="font-size:12px;color:#475569;text-align:center;margin:0 auto 14px;">
								<span data-wmk-preview-validity-text><?php echo esc_html( $validity_line ); ?></span>
							</p>
							<div class="wmk-pe-cta-wrap<?php echo '' !== $cta_label ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-cta-wrap>
								<a class="wmk-pe-cta" data-wmk-preview-cta href="#" style="<?php echo esc_attr( 'background:' . $accent . ';border-radius:' . $cta_radius . 'px;' ); ?>"><?php echo esc_html( $cta_label ); ?></a>
							</div>
						</div>
						<small class="<?php echo '' !== $preheader ? '' : 'wmk-preview-hidden '; ?>" data-wmk-preview-preheader-wrap style="display:block;text-align:center;padding:0 18px 16px;color:#94A3B8;font-size:11px;">
							<span data-wmk-preview-preheader><?php echo esc_html( $preheader ); ?></span>
						</small>
						<div class="wmk-pe-footer">
							<strong class="wmk-pe-footer-brand<?php echo '' !== $footer_brand ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-brand><?php echo esc_html( $footer_brand ); ?></strong>
							<div class="wmk-pe-footer-legal<?php echo '' !== $footer_legal ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-address><?php echo esc_html( $footer_legal ); ?></div>
							<div class="wmk-pe-unsub">
								<span data-wmk-preview-unsub-text><?php echo esc_html( $unsubscribe_text ); ?></span>
								<a href="#" data-wmk-preview-unsub-cta><?php echo esc_html( $unsubscribe_cta ); ?></a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * @param array<string, mixed> $data
	 */
	public static function coupon_optin( array $data ): void {
		$sample_name = (string) ( $data['sample_name'] ?? __( 'there', 'suitemkt3k' ) );
		$heading     = (string) ( $data['heading'] ?? '' );
		$subject     = (string) ( $data['subject'] ?? '' );
		if ( '' === $heading ) {
			$heading = $subject;
		}
		if ( '' === $heading ) {
			$heading = '—';
		}

		$body = (string) ( $data['body'] ?? '' );
		if ( '' === $body ) {
			$body = (string) ( $data['body_fallback'] ?? __( 'Your email body will appear here.', 'suitemkt3k' ) );
		} else {
			$body = str_replace(
				array( '{{name}}', '{{first_name}}', '{{code}}', '{{coupon_name}}', '{{marketing}}' ),
				array( $sample_name, $sample_name, 'SAMPLE10', __( 'Sample campaign', 'suitemkt3k' ), __( 'Thanks for signing up! Here is your exclusive discount.', 'suitemkt3k' ) ),
				$body
			);
		}
		$heading = str_replace(
			array( '{{name}}', '{{coupon_name}}', '{{code}}' ),
			array( $sample_name, __( 'Sample campaign', 'suitemkt3k' ), 'SAMPLE10' ),
			$heading
		);

		$accent         = UI::normalize_hex_public( (string) ( $data['accent'] ?? '#2AB2E5' ) );
		$logo_url       = (string) ( $data['logo_url'] ?? '' );
		$brand          = (string) ( $data['brand'] ?? '' );
		$preheader      = (string) ( $data['preheader'] ?? '' );
		$cta_label      = (string) ( $data['cta_label'] ?? '' );
		$unsubscribe_text = (string) ( $data['unsubscribe_text'] ?? '' );
		$unsubscribe_cta  = (string) ( $data['unsubscribe_cta'] ?? '' );
		if ( '' === $unsubscribe_text ) {
			$unsubscribe_text = __( 'You no longer want to receive emails like this?', 'suitemkt3k' );
		}
		if ( '' === $unsubscribe_cta ) {
			$unsubscribe_cta = __( 'Unsubscribe', 'suitemkt3k' );
		}

		$has_logo     = '' !== $logo_url;
		$cta_radius   = max( 0, min( 99, (int) ( $data['cta_radius'] ?? 10 ) ) );
		$header_text  = $brand;
		$footer_ctx   = Email_Renderer::footer_context();
		$footer_brand = (string) ( $data['footer_text'] ?? $footer_ctx['footer_text'] );
		$footer_legal = (string) ( $data['address'] ?? $footer_ctx['address'] );
		?>
		<div class="wmk-designer-preview" data-wmk-email-preview="coupon">
			<div class="wmk-card">
				<h3><?php esc_html_e( 'Preview', 'suitemkt3k' ); ?></h3>
				<div class="wmk-preview-frame">
					<div class="wmk-preview-email" data-wmk-preview-root>
						<div class="wmk-pe-logo-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-logo-wrap>
							<img class="wmk-pe-logo" data-wmk-preview-logo src="<?php echo esc_url( $logo_url ); ?>" alt="">
						</div>
						<div class="wmk-pe-accent-bar<?php echo $has_logo ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-accent-bar style="<?php echo $has_logo ? esc_attr( 'background:' . $accent . ';' ) : ''; ?>"></div>
						<div class="wmk-pe-header<?php echo $has_logo ? ' wmk-preview-hidden' : ''; ?>" data-wmk-preview-header style="<?php echo $has_logo ? '' : esc_attr( 'background:' . $accent . ';' ); ?>">
							<span data-wmk-preview-header-text><?php echo esc_html( $header_text ); ?></span>
						</div>
						<div class="wmk-pe-body">
							<h2 class="wmk-pe-heading" data-wmk-preview-heading><?php echo esc_html( $heading ); ?></h2>
							<p class="wmk-pe-paragraph" data-wmk-preview-body><?php echo esc_html( wp_strip_all_tags( $body ) ); ?></p>
							<div class="wmk-pe-cta-wrap<?php echo '' !== $cta_label ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-cta-wrap>
								<a class="wmk-pe-cta" data-wmk-preview-cta href="#" style="<?php echo esc_attr( 'background:' . $accent . ';border-radius:' . $cta_radius . 'px;' ); ?>"><?php echo esc_html( $cta_label ); ?></a>
							</div>
						</div>
						<small class="<?php echo '' !== $preheader ? '' : 'wmk-preview-hidden '; ?>" data-wmk-preview-preheader-wrap style="display:block;text-align:center;padding:0 18px 16px;color:#94A3B8;font-size:11px;">
							<span data-wmk-preview-preheader><?php echo esc_html( $preheader ); ?></span>
						</small>
						<div class="wmk-pe-footer">
							<strong class="wmk-pe-footer-brand<?php echo '' !== $footer_brand ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-brand><?php echo esc_html( $footer_brand ); ?></strong>
							<div class="wmk-pe-footer-legal<?php echo '' !== $footer_legal ? '' : ' wmk-preview-hidden'; ?>" data-wmk-preview-footer-address><?php echo esc_html( $footer_legal ); ?></div>
							<div class="wmk-pe-unsub">
								<span data-wmk-preview-unsub-text><?php echo esc_html( $unsubscribe_text ); ?></span>
								<a href="#" data-wmk-preview-unsub-cta><?php echo esc_html( $unsubscribe_cta ); ?></a>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
