<?php
namespace PixelioCartAbandonRecoveryMore\Admin\Services;

use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

class Pixelio_Cart_Abandon_Recovery_More_Consent_Service
{
	private $wordpress_wrapper;
	private $pixelio_util;
	private $user_consent_repo;

	public function __construct($wordpress_wrapper, $pixelio_util, $user_consent_repo)
	{
		$this->wordpress_wrapper = $wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
		$this->user_consent_repo = $user_consent_repo;
	}

	public function save_consent_from_post(): void
	{
		if (!$this->wordpress_wrapper->pixelio_wc_get_session('uuid') || !isset($_POST['pixelio-cart-abandon-recovery-more-email'])) {
			return;
		}

		$email = $this->wordpress_wrapper->pixelio_sanitize_text_field(
			$this->wordpress_wrapper->pixelio_wp_unslash($_POST['pixelio-cart-abandon-recovery-more-email'])
		);

		$rgpd_value = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked(
			'pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox'
		) && isset($_POST['pixelio-abandon-recovery-more-rgpd'])
			? $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($_POST['pixelio-abandon-recovery-more-rgpd']))
			: $this->wordpress_wrapper->pixelio_translate(
				'GDPR option was at that moment not active',
				'rivio-ai-cart-recovery-marketing-email-automation'
			);

		$this->user_consent_repo->pixelio_save_consent(
			$email,
			Pixelio_Cart_Abandon_Recovery_More_Util::$finalite_cart,
			$rgpd_value
		);
	}
}
