<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Services;


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\Models\Pixelio_Cart_Abandon_Recovery_More_Coupon_Model;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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


/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Email
 *
 * Handles email sending for abandoned carts and order review requests.
 * Core responsibilities:
 *  - Retrieve and validate recipients from POST data or WooCommerce orders
 *  - Generate coupons and prepare email content
 *  - Replace placeholders in email templates
 *  - Log sent emails
 *  - Generate recovery, review, and unsubscribe links
 *  - Process email queues and update status via API or local storage
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Service
 */
class Pixelio_Cart_Abandon_Recovery_More_Email_Coupon_Service
{
	/**
	 * WordPress abstraction layer (POST, sanitize, esc_html, etc).
	 *
	 * @var Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper
	 */
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;

	/**
	 * Pixelio utility service (options + business logic).
	 *
	 * @var Pixelio_Cart_Abandon_Recovery_More_Util
	 */
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;
	private Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $pixelio_woocommerce_wrapper;


	/**
	 * Constructor
	 *
	 * @param object $pixelio_wordpress_wrapper WordPress wrapper
	 * @param object $pixelio_util Plugin utility service
	 */
	public function __construct(Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper, Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util,
	pIXElio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $pixelio_woocommerce_wrapper)
	{
		$this->pixelio_wordpress_wrapper = $pixelio_wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
		$this->pixelio_woocommerce_wrapper = $pixelio_woocommerce_wrapper;
	}


	public function pixelio_maybe_create_coupon(array $data, array $pixelio_config, $pixelio_user_email): ?string
	{
		// todo	add a settings that can be enabled to disable coupon creation for IA or to set max discount for IA
//		if (empty($pixelio_config['coupon_dependency']) || !$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked($pixelio_config['coupon_dependency'])) {
//			return null;
//		}

		$pixelio_coupon = new Pixelio_Cart_Abandon_Recovery_More_Coupon_Model($this->pixelio_wordpress_wrapper,$this->pixelio_woocommerce_wrapper);
		return $pixelio_coupon->pixelio_cart_abandon_recovery_more_create_user_specific_coupon($pixelio_user_email,  $data['coupon_expiration'],  $data['coupon_value'], $data['coupon_type'] ?? 1,false);
	}

	public function pixelio_cart_abandon_recovery_more_create_coupon(string $pixelio_user_email,$discount,$expiration,$createDiscountForPopup): string
	{
		$pixelio_coupon = new Pixelio_Cart_Abandon_Recovery_More_Coupon_Model($this->pixelio_wordpress_wrapper,$this->pixelio_woocommerce_wrapper);
		return $pixelio_coupon->pixelio_cart_abandon_recovery_more_create_user_specific_coupon($pixelio_user_email,$expiration,$discount,1,$createDiscountForPopup);
	}

}
