<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Services;


use Pixelio_Cart_Abandon_Recovery_More\Segmentation\Service\Pixelio_Cart_Abandon_Recovery_More_Segmentation_Customer_Profile_Sync_Service;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository;
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_Sync_Service
{

	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;


	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Repository $cartRepo;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository $cartItemRepo;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository $emailRepo;

	public function __construct(
		$wordpress_wrapper,
		$pixelio_util,
		$cartRepo,
		$cartItemRepo,
		$emailRepo
	)
	{
		$this->pixelio_wordpress_wrapper = $wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
		$this->cartRepo = $cartRepo;
		$this->cartItemRepo = $cartItemRepo;
		$this->emailRepo = $emailRepo;

	}


	/**
	 * Sync plugin tables with remote API.
	 *
	 * @return void
	 */
	public function pixelio_sync_tables_cron(): void
	{
		if (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_decrypt_api_key("pixelio_api_key")) {
			return;
		}

		$cartRepo = $this->cartRepo ?? new Pixelio_Cart_Abandon_Recovery_More_Cart_Repository($this->pixelio_wordpress_wrapper);
		$cartItemRepo = $this->cartItemRepo ?? new Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository($this->pixelio_wordpress_wrapper);
		$emailRepo = $this->emailRepo ?? new Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository($this->pixelio_wordpress_wrapper);
		$sessionAndAnonymousDataRepo = $this->sessionDataRepo
			?? new Pixelio_Cart_Abandon_Recovery_More_Session_Repository($this->pixelio_wordpress_wrapper);



		$batch_data = [
			'cart' => $cartRepo->pixelio_get_not_yet_synced(),
			'cart_item' => $cartItemRepo->pixelio_get_not_yet_synced(),
			'email_templates' => $emailRepo->pixelio_get_not_yet_synced(),
			'session_data' => $sessionAndAnonymousDataRepo->pixelio_get_session_data_not_yet_synced(),
			'anonymous_events' => $sessionAndAnonymousDataRepo->pixelio_get_anonymous_event_not_yet_synced(),
		];

		if (!empty($batch_data)) {
			$api_url = $this->pixelio_util->pixelio_cart_abandon_recovery_more_get_url() . 'synchronize';
			$response = $this->pixelio_util->pixelio_cart_abandon_recovery_more_call_api($api_url, "POST", $batch_data);

			if (!$this->pixelio_wordpress_wrapper->pixelio_is_wp_error($response) ) {
				$cartRepo->pixelio_mark_synced($batch_data['cart']);
				$cartItemRepo->pixelio_mark_synced($batch_data['cart_item']);
				$emailRepo->pixelio_mark_synced($batch_data['email_templates']);
				$sessionAndAnonymousDataRepo->pixelio_mark_anonymous_event_synced($batch_data['anonymous_events']);
				$sessionAndAnonymousDataRepo->pixelio_mark_session_data_synced($batch_data['session_data']);
			}
		}
	}





}
