<?php
/**
 * Email Sender.
 *
 * Handles composing and sending HTML emails via wp_mail().
 *
 * @package Ugcify\Email
 */

namespace Ugcify\Email;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class EmailSender
 */
class EmailSender {

	/**
	 * Send an email based on a configured email type.
	 *
	 * @param string $email_type     The email type key (e.g. 'order_complete').
	 * @param string $recipient_email Recipient email address.
	 * @param array  $placeholder_data Data for placeholder replacement.
	 * @return bool Whether the email was sent successfully.
	 */
	public static function send( $email_type, $recipient_email, array $placeholder_data = array() ) {
		/**
		 * Short-circuit email sending. Return a bool to bypass real send.
		 *
		 * @param bool|null $pre              Default null (continue normally).
		 * @param string    $email_type       Email type key.
		 * @param string    $recipient_email  Recipient.
		 * @param array     $placeholder_data Data passed to send().
		 */
		if ( function_exists( 'apply_filters' ) ) {
			$pre = apply_filters( 'ugcify_email_pre_send', null, $email_type, $recipient_email, $placeholder_data );
			if ( null !== $pre ) {
				return (bool) $pre;
			}
		}

		// Check if this email type is enabled.
		if ( ! EmailSettings::is_enabled( $email_type ) ) {
			return false;
		}

		// Get email type configuration.
		$config = EmailSettings::get_email_type( $email_type );
		if ( ! $config ) {
			return false;
		}

		// Get global settings for headers and branding.
		$global = EmailSettings::get_global_settings();

		// Replace placeholders in subject and body.
		$subject = EmailPlaceholders::replace( $config['subject'], $placeholder_data );
		$body    = EmailPlaceholders::replace( $config['body'], $placeholder_data );

		// Wrap body in HTML template.
		$html_body = self::build_html_template( $body, $global );

		// Build email headers.
		$headers = self::build_headers( $global );

		// Send using wp_mail.
		return wp_mail( $recipient_email, $subject, $html_body, $headers );
	}

	/**
	 * Build HTML email template with branding.
	 *
	 * @param string $body_content The email body HTML.
	 * @param array  $global       Global email settings.
	 * @return string Complete HTML email.
	 */
	private static function build_html_template( $body_content, $global ) {
		$body_color    = esc_attr( $global['body_color'] );
		$heading_color = esc_attr( $global['heading_color'] );
		$text_color    = esc_attr( $global['text_color'] );
		$button_color  = esc_attr( $global['button_color'] );
		$link_color    = esc_attr( $global['link_color'] );
		$header_logo   = esc_url( $global['header_logo'] );
		$footer_text   = wp_kses_post( $global['footer_text'] );

		ob_start();
		?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title><?php echo esc_html( get_bloginfo( 'name' ) ); ?></title>
	<?php
	/*
	 * Inline <style> is intentional here. This is an HTML email template passed
	 * to wp_mail(), not a web page. Email clients do not load external stylesheets,
	 * so wp_enqueue_style() cannot be used. Inline styles in the <head> are the
	 * standard and required approach for HTML email.
	 */
	?>
	<style>
		body {
			margin: 0;
			padding: 0;
			background-color: <?php echo esc_attr( $body_color ); ?>;
			font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
			color: <?php echo esc_attr( $text_color ); ?>;
			line-height: 1.6;
		}
		.email-wrapper {
			max-width: 600px;
			margin: 0 auto;
			padding: 40px 20px;
		}
		.email-container {
			background-color: #ffffff;
			border-radius: 8px;
			overflow: hidden;
			box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
		}
		.email-header {
			padding: 30px 40px;
			text-align: center;
			border-bottom: 1px solid #eee;
		}
		.email-header img {
			max-width: 180px;
			max-height: 60px;
			height: auto;
		}
		.email-body {
			padding: 40px;
			color: <?php echo esc_attr( $text_color ); ?>;
		}
		.email-body h1,
		.email-body h2,
		.email-body h3 {
			color: <?php echo esc_attr( $heading_color ); ?>;
			margin-top: 0;
		}
		.email-body a {
			color: <?php echo esc_attr( $link_color ); ?>;
		}
		.email-body blockquote {
			border-left: 4px solid <?php echo esc_attr( $button_color ); ?>;
			margin: 16px 0;
			padding: 12px 20px;
			background: <?php echo esc_attr( $body_color ); ?>;
			border-radius: 0 4px 4px 0;
		}
		.email-footer {
			padding: 24px 40px;
			text-align: center;
			font-size: 13px;
			color: #999;
			border-top: 1px solid #eee;
		}
	</style>
</head>
<body>
	<div class="email-wrapper">
		<div class="email-container">
			<?php if ( ! empty( $header_logo ) ) : ?>
			<div class="email-header">
				<img src="<?php echo esc_url( $header_logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>">
			</div>
			<?php endif; ?>

			<div class="email-body">
				<?php echo wp_kses_post( $body_content ); ?>
			</div>

			<?php if ( ! empty( $footer_text ) ) : ?>
			<div class="email-footer">
				<?php echo wp_kses_post( $footer_text ); ?>
			</div>
			<?php endif; ?>
		</div>
	</div>
</body>
</html>
		<?php
		return ob_get_clean();
	}

	/**
	 * Build email headers array.
	 *
	 * @param array $global Global email settings.
	 * @return array Headers for wp_mail.
	 */
	private static function build_headers( $global ) {
		$headers = array(
			'Content-Type: text/html; charset=UTF-8',
		);

		$from_name  = sanitize_text_field( $global['from_name'] );
		$from_email = sanitize_email( $global['from_email'] );
		$reply_to   = sanitize_email( $global['reply_to'] );

		if ( $from_name && $from_email ) {
			$headers[] = sprintf( 'From: %s <%s>', $from_name, $from_email );
		}

		if ( $reply_to ) {
			$headers[] = sprintf( 'Reply-To: %s', $reply_to );
		}

		return $headers;
	}
}
