<?php

namespace PixelioCartAbandonRecoveryMore\Frontend\Listeners;

use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Services\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service;

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

class Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Listener
{
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;
	private 		Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $woocommerce_wrapper;

	private Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service $wishlist_service;
	private Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service $unsubscribe_service;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service $email_Logger_Service;

	public function __construct(
		Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper,
		Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $woocommerce_wrapper,
		Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service $email_Logger_Service,
		Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service $unsubscribe_Service,
		Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service $wishlist_service
	) {
		$this->wordpress_wrapper = $wordpress_wrapper;
		$this->woocommerce_wrapper = $woocommerce_wrapper;
		$this->email_Logger_Service = $email_Logger_Service;
		$this->unsubscribe_service = $unsubscribe_Service;
		$this->wishlist_service = $wishlist_service;
	}

	public function register(): void
	{
		$this->wordpress_wrapper->pixelio_add_action(
			'wishlist_item_added',
			[$this, 'send_recovery_email'],
			10,
			2
		);

	}




	public function send_recovery_email(string $email, array $identity): void
	{
		$token = $this->wishlist_service->create_or_get_recovery_token($email, $identity);

		$url = $this->wordpress_wrapper->pixelio_add_query_arg(
			[
				'pixelio_wishlist_token' => $token,
			],
			$this->woocommerce_wrapper->pixelio_wc_get_page_permalink('shop')
		);

		$subject = $this->wordpress_wrapper->pixelio_translate(
			'Your wishlist recovery link'
		);


		$headers = [
			'Content-Type: text/html; charset=UTF-8',
		];

		if ($this->has_recovery_email_been_sent($email)) {
			return;
		}
		$unsubscribe_url = $this->unsubscribe_service->generate_unsubscribe_link($email);
		$message = $this->get_wishlist_recovery_email_template(
			$this->wordpress_wrapper->pixelio_esc_url($url),
			$unsubscribe_url
		);

		$this->wordpress_wrapper->pixelio_wp_mail(
			$email,
			$subject,
			$message,
			$headers
		);

	}

	public function get_wishlist_recovery_email_template(
		string $wishlist_url,
		string $unsubscribe_url
	): string {

		$wishlist_url     = esc_url($wishlist_url);
		$unsubscribe_url = esc_url($unsubscribe_url);

		ob_start();
		?>

		<!DOCTYPE html>
		<html <?php language_attributes(); ?>>
		<head>
			<meta charset="<?php bloginfo('charset'); ?>">
			<meta name="viewport" content="width=device-width, initial-scale=1.0">
			<title><?php echo esc_html__('Your wishlist is waiting for you', 'pixelio'); ?></title>
		</head>

		<body style="margin:0;padding:0;background:#f4f4f5;font-family:Arial,sans-serif;color:#111827;">
		<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#f4f4f5;padding:40px 20px;">
			<tr>
				<td align="center">

					<table width="600" cellpadding="0" cellspacing="0" border="0" style="width:100%;max-width:600px;background:#ffffff;border-radius:24px;overflow:hidden;box-shadow:0 8px 30px rgba(0,0,0,0.06);">
						<tr>
							<td style="padding:52px 40px 24px;text-align:center;">

								<div style="display:inline-block;padding:8px 16px;background:#f3f4f6;border-radius:999px;font-size:12px;font-weight:600;letter-spacing:.04em;color:#6b7280;text-transform:uppercase;margin-bottom:24px;">
									<?php echo esc_html__('Wishlist', 'pixelio'); ?>
								</div>

								<h1 style="margin:0;font-size:34px;line-height:1.2;font-weight:700;color:#111827;">
									<?php echo esc_html__('Your wishlist is still here ✨', 'pixelio'); ?>
								</h1>

								<p style="margin:20px 0 0;font-size:16px;line-height:1.7;color:#6b7280;">
									<?php echo esc_html__('You saved a few items you liked. Come back anytime to continue where you left off.', 'pixelio'); ?>
								</p>

							</td>
						</tr>

						<tr>
							<td style="padding:16px 40px 44px;text-align:center;">
								<a href="<?php echo $wishlist_url; ?>" style="display:inline-block;padding:16px 36px;background:#111827;color:#ffffff;text-decoration:none;border-radius:14px;font-size:15px;font-weight:600;">
									<?php echo esc_html__('View my wishlist', 'pixelio'); ?>
								</a>
							</td>
						</tr>

						<tr>
							<td style="padding:0 40px 44px;text-align:center;">
								<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background:#fafafa;border:1px solid #f1f1f1;border-radius:20px;">
									<tr>
										<td style="padding:28px 24px;text-align:center;">
											<p style="margin:0;font-size:15px;line-height:1.7;color:#6b7280;">
												<?php echo esc_html__('Some favorites can sell out quickly. If something caught your eye, now is a good time to take another look.', 'pixelio'); ?>
											</p>
										</td>
									</tr>
								</table>
							</td>
						</tr>

						<tr>
							<td style="padding:0 40px 40px;text-align:center;">
								<p style="margin:0;font-size:13px;line-height:1.8;color:#9ca3af;">
									<?php echo esc_html__('You are receiving this email because you added items to your wishlist.', 'pixelio'); ?>
								</p>

								<p style="margin:12px 0 0;font-size:13px;">
									<a href="<?php echo $unsubscribe_url; ?>" style="color:#9ca3af;text-decoration:underline;">
										<?php echo esc_html__('Unsubscribe', 'pixelio'); ?>
									</a>
								</p>
							</td>
						</tr>
					</table>

				</td>
			</tr>
		</table>
		</body>
		</html>

		<?php

		return ob_get_clean();
	}

	public function has_recovery_email_been_sent(string $email)
	{
		if ($this->email_Logger_Service->pixelio_email_is_already_sent_for_wishlist_email($email, 3)) { //3 -> wishlist_recovery
			return true;
		}
		$this->email_Logger_Service->pixelio_log_email(
			[
				'cart_id'     => 0,
				'subject'     => 'Wishlist recovery',
				'template_id' => 0,
			],
			[
				'type_of_email' => '3',
			],
			$email
		);
		return false;
	}


}
