<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Services;

use PixelioCartAbandonRecoveryMore\Admin\Services\Tracking\Pixelio_Cart_Abandon_Recovery_More_Event_Dto;
use PixelioCartAbandonRecoveryMore\Admin\Services\Tracking\Pixelio_Cart_Abandon_Recovery_More_Tracking_Context;
use PixelioCartAbandonRecoveryMore\Admin\Services\Tracking\Pixelio_Cart_Abandon_Recovery_More_Consent_Tracking_Strategy;
use PixelioCartAbandonRecoveryMore\Admin\Services\Tracking\Pixelio_Cart_Abandon_Recovery_More_Anonymous_Aggregate_Tracking_Strategy;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Session_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

class Pixelio_Cart_Abandon_Recovery_More_Session_Service
{
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Session_Repository $session_model;
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;

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

	public function save_page_view(): void
	{
		if (!$this->can_track_current_page()) {
			return;
		}

		$product_id = null;
		$parent_product_id = null;

		if ($this->wordpress_wrapper->pixelio_is_product()) {
			$product_id = (int) $this->wordpress_wrapper->pixelio_get_current_post_id();
			$parent_product_id = $this->get_parent_product_id($product_id);
		}

		$event = new Pixelio_Cart_Abandon_Recovery_More_Event_Dto([
			'event_type' => 'page_view',
			'uuid' => $this->get_uuid_if_allowed(),
			'product_id' => $product_id,
			'parent_product_id' => $parent_product_id,
			'page_url' => $this->get_current_full_url(),
			'page_type' => $this->get_page_type(),
			'tracking_mode' => $this->get_tracking_mode(),
		]);

		$this->get_tracking_context()->track($event);
	}

	private function get_tracking_context(): Pixelio_Cart_Abandon_Recovery_More_Tracking_Context
	{
		if ($this->has_tracking_consent()) {
			return new Pixelio_Cart_Abandon_Recovery_More_Tracking_Context(
				new Pixelio_Cart_Abandon_Recovery_More_Consent_Tracking_Strategy($this->session_model)
			);
		}

		return new Pixelio_Cart_Abandon_Recovery_More_Tracking_Context(
			new Pixelio_Cart_Abandon_Recovery_More_Anonymous_Aggregate_Tracking_Strategy($this->session_model)
		);
	}

	private function has_tracking_consent(): bool
	{
		return
			$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox')
		&& isset($_COOKIE['pixelio_marketing_consent'])
			&& $_COOKIE['pixelio_marketing_consent'] === '1';
	}
	private function get_tracking_mode(): string
	{
		if (
			!$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked(
				'pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox'
			)
		) {
			return 'gdpr_disabled';
		}

		if (
			isset($_COOKIE['pixelio_marketing_consent'])
			&& $_COOKIE['pixelio_marketing_consent'] === '1'
		) {
			return 'consent_granted';
		}

		return 'anonymous';
	}
	private function get_uuid_if_allowed(): ?string
	{
		if (!$this->has_tracking_consent()) {
			return null;
		}

		$uuid = $this->wordpress_wrapper->pixelio_wc_get_session('uuid');

		if (empty($uuid)) {
			return null;
		}

		return $this->wordpress_wrapper->pixelio_sanitize_text_field($uuid);
	}

	private function can_track_current_page(): bool
	{
		if ($this->wordpress_wrapper->pixelio_is_admin()) {
			return false;
		}

		if (
			$this->wordpress_wrapper->pixelio_is_category()
			|| $this->wordpress_wrapper->pixelio_is_404()
			|| $this->wordpress_wrapper->pixelio_is_tag()
			|| $this->wordpress_wrapper->pixelio_is_archive()
		) {
			return false;
		}

		return true;
	}

	private function get_parent_product_id(int $product_id): ?int
	{
		$previous_product_id = isset($_COOKIE['parent_product_id'])
			? (int) $_COOKIE['parent_product_id']
			: null;

		$this->wordpress_wrapper->pixelio_setcookie(
			'parent_product_id',
			(string) $product_id,
			time() + (86400 * 30),
			COOKIEPATH,
			COOKIE_DOMAIN
		);

		if ($previous_product_id && $previous_product_id !== $product_id) {
			return $previous_product_id;
		}

		return null;
	}

	private function get_page_type(): string
	{
		if ($this->wordpress_wrapper->pixelio_is_product()) {
			return 'product';
		}

		if ($this->wordpress_wrapper->pixelio_is_cart()) {
			return 'cart';
		}

		if ($this->wordpress_wrapper->pixelio_is_checkout()) {
			return 'checkout';
		}

		return 'page';
	}

	private function get_current_full_url(): string
	{
		$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
		$host = $_SERVER['HTTP_HOST'] ?? '';
		$request_uri = $_SERVER['REQUEST_URI'] ?? '';

		return esc_url_raw($protocol . '://' . $host . $request_uri);
	}
}
