<?php
/**
 * Base email view for email notifications.
 *
 * @package Awesomemotive\Rewardswp\Views\Notifications
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Views\Notifications;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractView;
use function Awesomemotive\Rewardswp\Helpers\kses;


/**
 * Base Email View.
 *
 * @since 1.0.0
 */
class BaseEmailView extends AbstractView {
	/**
	 * Render the template.
	 *
	 * @since 1.0.0
	 *
	 * @param array $args {
	 *     Array of arguments for configuring the email template.
	 *
	 *     @type string $email_title        The content to be used in the <title> tag.
	 *     @type string $email_content      The email content. It can contain HTML.
	 *     @type string $email_footer       The footer text.
	 *     @type string $header_text        The header text. Default 'Hello'.
	 *     @type string $body_text          The body text. Default ''.
	 *     @type string $brand_color        The brand color for buttons and links. Default '#007cba'.
	 *     @type string $button_text        The button text. Default 'Click here'.
	 *     @type string $button_url         The button URL. Default '#'.
	 *     @type string $footer_text        The footer text. Default ''.
	 *     @type string $background_color   The background color. Default '#f4f4f7'.
	 *     @type string $foreground_color   The foreground color. Default '#ffffff'.
	 *     @type string $text_color         The main text color. Default '#2c3e50'.
	 *     @type string $secondary_color    The secondary text color. Default '#7f8c8d'.
	 *     @type string $border_color       The border color for elements. Default '#e9ecef'.
	 *     @type string $font_family        Font family. Default system fonts.
	 *     @type array  $banner_data        Banner data array with url, width, height, alt, etc.
	 *     @type array  $logo_data          Logo data array with url, width, height, alt, etc.
	 * }
	 *
	 * @return void
	 */
	public function render( array $args = [] ): void {
		$args = wp_parse_args(
			$args,
			[
				'email_title'             => '',
				'email_content'           => '',
				'email_footer'            => sprintf(
					/* translators: %1$s is the site's URL, %2$s is the site's name */
					__( 'Sent by <a href="%1$s">%2$s</a>', 'rewardswp' ),
					esc_url( home_url() ),
					get_bloginfo( 'name' )
				),
				'header_text'             => __( 'Hello', 'rewardswp' ),
				'body_text'               => '',
				'brand_color'             => '#2563eb', // Fallback - should be overridden by notification
				'brand_text_color'        => '#ffffff', // Fallback - should be overridden by notification
				'button_text'             => __( 'Click here', 'rewardswp' ),
				'button_url'              => '#',
				'footer_text'             => '',
				'background_color'        => '#eceef1',
				'foreground_color'        => '#ffffff',
				'text_color'              => '#2c3e50',
				'secondary_color'         => '#7f8c8d',
				'border_color'            => '#e9ecef',
				'font_family'             => '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif',
				'banner_data'             => null,
				'logo_data'               => null,
				// Only the preview flow should set this to true via filter.
				'show_banner_placeholder' => false,
				'show_logo_placeholder'   => false,
			]
		);
		?>

		<!DOCTYPE html>
		<html lang="<?php bloginfo( 'language' ); ?>">
			<head>
				<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
				<meta http-equiv="X-UA-Compatible" content="IE=edge">
				<meta name="viewport" content="width=device-width, initial-scale=1.0">
				<title><?php echo esc_attr( $args['email_title'] ); ?></title>
				<style>
					/* Essential styles only - maximum email client compatibility */
					body, html {
						margin: 0;
						padding: 0;
						width: 100%;
						font-family: <?php echo html_entity_decode( esc_html( $args['font_family'] ) ); /* phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Already Escaped */ ?>;
						background-color: <?php echo esc_html( $args['background_color'] ); ?>;
						color: <?php echo esc_html( $args['text_color'] ); ?>;
						font-size: 16px;
						line-height: 1.6;
						-webkit-text-size-adjust: 100%;
						-ms-text-size-adjust: 100%;
					}

					/* Essential table resets for email compatibility */
					table {
						border-collapse: collapse;
						mso-table-lspace: 0pt;
						mso-table-rspace: 0pt;
					}

					img {
						border: 0;
						height: auto;
						line-height: 100%;
						outline: none;
						text-decoration: none;
						-ms-interpolation-mode: bicubic;
					}

					/* Basic typography - avoid complex selectors */
					h1, h2, h3, h4, h5, h6 {
						margin: 0 0 16px 0;
						line-height: 1.3;
						font-weight: normal;
					}

					p {
						margin: 0 0 16px 0;
					}

					/* Simple link styles */
					a {
						color: <?php echo esc_html( $args['brand_color'] ); ?>;
						text-decoration: underline;
					}

					/* Button link override */
					a[style*="background-color"] {
						text-decoration: none !important;
					}

					/* Remove problematic styles - we'll use inline instead */
				</style>
			</head>
			<body style="margin: 0; padding: 0; width: 100%; font-family: <?php echo esc_attr( $args['font_family'] ); ?>; background-color: <?php echo esc_attr( $args['background_color'] ); ?>; color: <?php echo esc_attr( $args['text_color'] ); ?>;">
			<!-- Main container table -->
			<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; background-color: <?php echo esc_attr( $args['background_color'] ); ?>;">
				<tr>
					<td align="center" style="padding: 20px 10px;">
						<!-- Email container table -->
						<table role="presentation" width="600" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; max-width: 600px; width: 100%; background-color: <?php echo esc_attr( $args['foreground_color'] ); ?>; border-radius: 16px; overflow: hidden;">

							<!-- Email Logo Section -->
							<?php if ( ! empty( $args['logo_data'] ) && is_array( $args['logo_data'] ) && ! empty( $args['logo_data']['url'] ) ) : ?>
							<tr>
								<td style="text-align: center; padding: 24px;">
									<img src="<?php echo esc_url( $args['logo_data']['url'] ); ?>"
										alt="<?php echo esc_attr( $args['logo_data']['alt'] ); ?>"
										style="max-width: 100%; height: auto; display: block; margin: 0 auto; border: 0; outline: none; text-decoration: none;"
										<?php if ( ! empty( $args['logo_data']['width'] ) ) : ?>
										width="<?php echo esc_attr( $args['logo_data']['width'] ); ?>"
										<?php endif; ?>
										<?php if ( ! empty( $args['logo_data']['height'] ) ) : ?>
										height="<?php echo esc_attr( $args['logo_data']['height'] ); ?>"
										<?php endif; ?>>
								</td>
							</tr>
							<?php elseif ( ! empty( $args['show_logo_placeholder'] ) ) : ?>
							<tr>
								<td style="padding: 24px;">
									<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; max-width: 360px; min-height: 120px; margin: 0 auto;">
										<tr>
											<td style="height: 120px; border: 1px dashed #d1d5db; border-radius: 8px; text-align: center; color: #9ca3af; vertical-align: middle;">
												<div style="font-size: 14px;"><?php esc_html_e( 'Logo', 'rewardswp' ); ?><br>360px <?php esc_html_e( 'by', 'rewardswp' ); ?> 120px</div>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<?php endif; ?>

							<!-- Email Banner Section -->
							<?php if ( ! empty( $args['banner_data'] ) && is_array( $args['banner_data'] ) && ! empty( $args['banner_data']['url'] ) ) : ?>
							<tr>
								<td style="text-align: center; padding: 0;">
									<img src="<?php echo esc_url( $args['banner_data']['url'] ); ?>"
										alt="<?php echo esc_attr( $args['banner_data']['alt'] ); ?>"
										width="100%"
										style="width: 100%; max-width: 100%; height: auto; display: block; margin: 0; border: 0; outline: none; text-decoration: none;">
								</td>
							</tr>
							<?php elseif ( ! empty( $args['show_banner_placeholder'] ) ) : ?>
							<tr>
								<td style="padding: 0;">
									<table role="presentation" cellpadding="0" cellspacing="0" border="0" style="width: 100%; max-width: 600px; min-height: 180px; margin: 0 auto;">
										<tr>
											<td style="padding: 24px 0; border: 1px dashed #d1d5db; border-radius: 8px; text-align: center; color: #9ca3af;">
												<div style="font-size: 14px;"><?php esc_html_e( 'Banner', 'rewardswp' ); ?> <br>600px <?php esc_html_e( 'by', 'rewardswp' ); ?> 180px</div>
											</td>
										</tr>
									</table>
								</td>
							</tr>
							<?php endif; ?>

														<!-- Email Content Section -->
							<tr>
								<td style="padding: 36px 48px 24px; color: <?php echo esc_attr( $args['text_color'] ); ?>; font-family: <?php echo esc_attr( $args['font_family'] ); ?>; font-size: 16px; line-height: 1.6;">
									<?php
									// Replace brand color placeholders in the email content.
									$processed_content = str_replace(
										[ '{{brand_color}}', '{{brand_text_color}}' ],
										[ esc_attr( $args['brand_color'] ), esc_attr( $args['brand_text_color'] ) ],
										$args['email_content']
									);
									echo wp_kses( $processed_content, kses() );
									?>
								</td>
							</tr>
						</table>

						<!-- Email Footer Section -->
						<table role="presentation" width="600" cellpadding="0" cellspacing="0" border="0" style="border-collapse: collapse; mso-table-lspace: 0pt; mso-table-rspace: 0pt; max-width: 600px; width: 100%; margin-top: 20px;">
							<tr>
								<td style="padding: 16px 24px; font-size: 12px; text-align: center; color: <?php echo esc_attr( $args['secondary_color'] ); ?>; font-family: <?php echo esc_attr( $args['font_family'] ); ?>;">
									<?php echo wp_kses( $args['email_footer'], kses() ); ?>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
			</body>
		</html>

		<?php
	}
}
