<?php
/**
 * Initializes the Pixelio Cart Abandon Recovery More plugin.
 *
 * Handles plugin activation, deactivation, uninstall, and initialization of
 * admin and public controllers. Also manages database helper instance.
 *
 * PHP version 8.0+
 *
 * @package  PixelioCartAbandonRecoveryMore
 * @version  0.1
 */

namespace PixelioCartAbandonRecoveryMore;


use admin\segmentations\collectors\Pixelio_Cart_Abandon_Recovery_More_Segmentation_Avg_Order_Collectors;
use admin\segmentations\collectors\Pixelio_Cart_Abandon_Recovery_More_Segmentation_Order_Collectors;
use admin\segmentations\collectors\Pixelio_Cart_Abandon_Recovery_More_Segmentation_Recency_Collectors;
use PixelioCartAbandonRecoveryMore\Admin\Controllers\Pixelio_Cart_Abandon_Recovery_More_Admin_Ajax_Controller;
use PixelioCartAbandonRecoveryMore\Admin\Controllers\Pixelio_Cart_Abandon_Recovery_More_Admin_Controller;
use PixelioCartAbandonRecoveryMore\Admin\Controllers\Pixelio_Cart_Abandon_Recovery_More_Admin_Job_Controller;
use PixelioCartAbandonRecoveryMore\Frontend\Controllers\Pixelio_Cart_Abandon_Recovery_More_Ajax_Event_Data_Tracking_Controller;
use PixelioCartAbandonRecoveryMore\Admin\Controllers\Pixelio_Cart_Abandon_Recovery_More_Rgpd_Ajax_Controller;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Config_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Domain_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Builder_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Coupon_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Sender_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Variable_Replacer_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Notice_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Order_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Segmentation_Customer_Profile_Sync_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Sync_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Update_Service;
use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Uuid_Service;
use PixelioCartAbandonRecoveryMore\Admin\Views\Pixelio_Cart_Abandon_Recovery_More_Update_Renderer;
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\Controllers\Pixelio_Cart_Abandon_Recovery_More_Ajax_Controller;
use PixelioCartAbandonRecoveryMore\Frontend\Controllers\Pixelio_Cart_Abandon_Recovery_More_Cart_Controller;
use PixelioCartAbandonRecoveryMore\Frontend\controllers\Pixelio_Cart_Abandon_Recovery_More_Frontend_Controller;
use PixelioCartAbandonRecoveryMore\Frontend\Dispatchers\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Event_Dispatcher;
use PixelioCartAbandonRecoveryMore\Frontend\Listeners\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Listener;
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_Logs_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\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Contact_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Services\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service;
use PixelioCartAbandonRecoveryMore\Includes\Db\Pixelio_Cart_Abandon_Recovery_More_DB_helper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;
use PixelioCartAbandonRecoveryMore\Includes\Validators\Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Validator;

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

/**
 * Initializes the Pixelio Cart Abandon Recovery More plugin.
 *
 * Handles plugin activation, deactivation, uninstall, and initialization of
 * admin and public controllers. Also manages database helper instance.
 *
 * PHP version 8.0+
 *
 * @package  PixelioCartAbandonRecoveryMore
 */
class Pixelio_Cart_Abandon_Recovery_More_Init
{

	/**
	 * Database helper instance.
	 *
	 * @var Pixelio_Cart_Abandon_Recovery_More_DB_helper
	 */
	private $db_helper;

	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_Woocommerce_Wrapper $pixelio_woocommerce_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Repository $pixelio_cart_model;
	private Pixelio_Cart_Abandon_Recovery_More_Sync_Service $pixelio_sync_service;
	private Pixelio_Cart_Abandon_Recovery_More_Domain_Service $pixelio_domain_service;
	private Pixelio_Cart_Abandon_Recovery_More_Config_Service $pixelio_config_service;
	private Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Service $pixelio_auto_service;
	private Pixelio_Cart_Abandon_Recovery_More_Notice_Service $pixelio_notice;
	private Pixelio_Cart_Abandon_Recovery_More_Update_Service $pixelio_update_service;
	private Pixelio_Cart_Abandon_Recovery_More_Update_Renderer $pixelio_update_popup_renderer;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Builder_Service $pixelio_email_service;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Variable_Replacer_Service $pixelio_email_replacer;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service $pixelio_email_logger;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Sender_Service $pixelio_email_sender;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository $pixelio_cart_item;
	private Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository $pixelio_uuid_repository;

	private Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository $pixelio_email_template_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Logs_Repository $pixelio_email_log_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Coupon_Service $pixelio_email_coupon;
	private Pixelio_Cart_Abandon_Recovery_More_Order_Service $order_service;
	private Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository $user_consent_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Uuid_Service $pixelio_uuid_service;
	private Pixelio_Cart_Abandon_Recovery_More_Segmentation_Customer_Profile_Sync_Service $profile_scoring_sync_service;
	private Pixelio_Cart_Abandon_Recovery_More_User_Contact_Repository $contact_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service $pixelio_wishlist_service;
	private Pixelio_Cart_Abandon_Recovery_More_Wishlist_Repository $pixelio_wishlist_repository;
	private Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Validator $pixelio_wishlist_validator;
	private Pixelio_Cart_Abandon_Recovery_More_Wishlist_Event_Dispatcher $pixelio_wishlist_dispatcher;
	private Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service $unsuscribe_service;
	private Pixelio_Cart_Abandon_Recovery_More_Session_Repository $pixelio_session_model;

	/**
	 * Constructor.
	 *
	 * Creates the database helper instance and ensures database tables exist.
	 *
	 * @return void
	 */
	public function __construct()
	{
		$this->pixelio_wordpress_wrapper = new Pixelio_Cart_Abandon_Recovery_More_WordPress_Wrapper();
		$this->pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util($this->pixelio_wordpress_wrapper);
		$this->pixelio_woocommerce_wrapper = new Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper();
		$this->order_service = new Pixelio_Cart_Abandon_Recovery_More_Order_Service($this->pixelio_woocommerce_wrapper, $this->pixelio_util,
			$this->pixelio_wordpress_wrapper);
		$this->pixelio_cart_item = new Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_cart_model = new Pixelio_Cart_Abandon_Recovery_More_Cart_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_domain_service = new Pixelio_Cart_Abandon_Recovery_More_Domain_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util);
		$this->pixelio_config_service = new Pixelio_Cart_Abandon_Recovery_More_Config_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util);
		$this->pixelio_auto_service = new Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util);
		$this->pixelio_notice = new Pixelio_Cart_Abandon_Recovery_More_Notice_Service($this->pixelio_wordpress_wrapper);
		$this->pixelio_update_service = new Pixelio_Cart_Abandon_Recovery_More_Update_Service($this->pixelio_util,$this->pixelio_wordpress_wrapper);
		$this->pixelio_update_popup_renderer = new Pixelio_Cart_Abandon_Recovery_More_Update_Renderer();

		$this->pixelio_email_replacer = new Pixelio_Cart_Abandon_Recovery_More_Email_Variable_Replacer_Service($this->pixelio_wordpress_wrapper, $this->pixelio_woocommerce_wrapper, $this->pixelio_cart_model, $this->pixelio_cart_item);
		$this->pixelio_email_sender = new Pixelio_Cart_Abandon_Recovery_More_Email_Sender_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util,
			$this->pixelio_email_replacer);
		$this->pixelio_wishlist_repository= new Pixelio_Cart_Abandon_Recovery_More_Wishlist_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_wishlist_validator = new Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Validator();
		$this->pixelio_wishlist_dispatcher = new Pixelio_Cart_Abandon_Recovery_More_Wishlist_Event_Dispatcher();

		$this->pixelio_email_coupon = new Pixelio_Cart_Abandon_Recovery_More_Email_Coupon_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util,$this->pixelio_woocommerce_wrapper);
		$this->pixelio_email_log_repo = new Pixelio_Cart_Abandon_Recovery_More_Email_Logs_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_email_template_repo = new Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_sync_service = new Pixelio_Cart_Abandon_Recovery_More_Sync_Service($this->pixelio_wordpress_wrapper, $this->pixelio_util,$this->pixelio_cart_model,$this->pixelio_cart_item,$this->pixelio_email_template_repo);
		$this->pixelio_uuid_repository = new Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_email_logger = new Pixelio_Cart_Abandon_Recovery_More_Email_Logger_Service($this->pixelio_wordpress_wrapper,
		$this->pixelio_email_log_repo);
		$this->pixelio_email_service = new Pixelio_Cart_Abandon_Recovery_More_Email_Builder_Service($this->pixelio_wordpress_wrapper,
			$this->pixelio_util,
			$this->pixelio_cart_model, $this->pixelio_email_coupon, $this->pixelio_email_logger, $this->pixelio_email_sender,
			$this->pixelio_email_replacer, $this->pixelio_email_log_repo, $this->pixelio_email_template_repo);
		$this->user_consent_repo = new Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository($this->pixelio_wordpress_wrapper);
		$this->pixelio_uuid_service = new Pixelio_Cart_Abandon_Recovery_More_Uuid_Service($this->pixelio_wordpress_wrapper,
			$this->user_consent_repo,$this->pixelio_util,$this->pixelio_uuid_repository,$this->pixelio_woocommerce_wrapper);
		$this->db_helper = new Pixelio_Cart_Abandon_Recovery_More_DB_helper($this->pixelio_wordpress_wrapper, $this->pixelio_util);;
		$this->contact_repo = new Pixelio_Cart_Abandon_Recovery_More_User_Contact_Repository($this->pixelio_wordpress_wrapper);
		$this->unsuscribe_service = new Pixelio_Cart_Abandon_Recovery_More_Unsubscribe_Service($this->pixelio_wordpress_wrapper, $this->user_consent_repo);

		$this->pixelio_wishlist_service = new Pixelio_Cart_Abandon_Recovery_More_Wishlist_Service($this->pixelio_wishlist_validator,
			$this->pixelio_wordpress_wrapper,
			$this->pixelio_uuid_service,
			$this->contact_repo);
		$wishlist_email_listener =
			new Pixelio_Cart_Abandon_Recovery_More_Wishlist_Email_Listener(
				$this->pixelio_wordpress_wrapper,
				$this->pixelio_woocommerce_wrapper,
				$this->pixelio_email_logger,
				$this->unsuscribe_service,
				$this->pixelio_wishlist_service
			);

		$wishlist_email_listener->register();
		$this->profile_scoring_sync_service = new Pixelio_Cart_Abandon_Recovery_More_Segmentation_Customer_Profile_Sync_Service([
			new Pixelio_Cart_Abandon_Recovery_More_Segmentation_Order_Collectors($this->pixelio_wordpress_wrapper),
			new Pixelio_Cart_Abandon_Recovery_More_Segmentation_Avg_Order_Collectors($this->pixelio_wordpress_wrapper),
			new Pixelio_Cart_Abandon_Recovery_More_Segmentation_Recency_Collectors($this->pixelio_wordpress_wrapper),
		], $this->pixelio_util,$this->pixelio_wordpress_wrapper,$this->contact_repo);

	}

	/**
	 * Handles plugin deactivation.
	 *
	 * Clears UUID cookie, deletes all plugin data, and removes scheduled jobs.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_deactivation(): void
	{
		if ($this->pixelio_wordpress_wrapper->pixelio_wc_get_session('uuid')) {
			$this->pixelio_woocommerce_wrapper->pixelio_wc_set_session('uuid',null);
		}
		$this->db_helper->pixelio_cart_abandon_recovery_more_delete_all_data();
		$job_controller = new Pixelio_Cart_Abandon_Recovery_More_Admin_Job_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
			$this->pixelio_email_service, $this->pixelio_sync_service,$this->profile_scoring_sync_service );;
		$job_controller->pixelio_cart_abandon_recovery_more_remove_schedule();
	}


	/**
	 * Handles plugin activation.
	 *
	 * Placeholder for future activation logic.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_activation(): void
	{
		$this->db_helper->pixelio_cart_abandon_recovery_more_create_db();
		$page_title = __('Unsubscribe', 'rivio-ai-cart-recovery-marketing-email-automation');
		$page_content = '[pixelio_unsubscribe]';

		$pages = get_posts(array(
			'post_type' => 'page',
			'title' => $page_title,
			'posts_per_page' => 1,
			'post_status' => 'publish',
		));

		if (!empty($pages)) {
			$page_check = $pages[0];
			update_option('pixelio_unsubscribe_page_id', $page_check->ID);
		} else {
			$page_id = wp_insert_post(array(
				'post_title' => $page_title,
				'post_content' => $page_content,
				'post_status' => 'publish',
				'post_type' => 'page',
				'post_author' => 1,
			));
			update_option('pixelio_unsubscribe_page_id', $page_id);
		}

		$page_title_error = __('Unsubscribe Error', 'rivio-ai-cart-recovery-marketing-email-automation');
		$page_content_error = '[pixelio_error]';

		$pages_error = get_posts(array(
			'post_type' => 'page',
			'title' => $page_title_error,
			'posts_per_page' => 1,
			'post_status' => 'publish',
		));

		if (!empty($pages_error)) {
			$page_check_error = $pages_error[0];
			update_option('pixelio_error_page_id', $page_check_error->ID);
		} else {
			$page_id_error = wp_insert_post(array(
				'post_title' => $page_title_error,
				'post_content' => $page_content_error,
				'post_status' => 'publish',
				'post_type' => 'page',
				'post_author' => 1,
			));
			update_option('pixelio_error_page_id', $page_id_error);
		}
		$this->maybe_create_wishlist_page();
	}

	public function maybe_create_wishlist_page(): void
	{
		$page_slug = 'pixelio-wishlist';

		$existing_page = get_page_by_path($page_slug);

		if ($existing_page) {
			return;
		}

		wp_insert_post([
			'post_title'   => $this->pixelio_wordpress_wrapper->pixelio_esc_html__('My Wishlist', ),
			'post_name'    => $page_slug,
			'post_content' => '[pixelio_wishlist]',
			'post_status'  => 'publish',
			'post_type'    => 'page',
		]);
	}

	/**
	 * Handles plugin uninstall.
	 *
	 * Deletes all plugin data and triggers uninstall cleanup actions.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_uninstall(): void
	{
		$this->db_helper->pixelio_cart_abandon_recovery_more_delete_all_data();
		pixelio_carm_fss()->add_action('after_uninstall', 'pixelio_carm_fss_uninstall_cleanup');
	}

	/**
	 * Initializes the plugin.
	 *
	 * Sets plugin URLs and directory paths. Initializes admin and public
	 * controllers based on the context (admin or frontend).
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_init(): void
	{

		Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_url = plugin_dir_url(__FILE__);
		Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_dir_path = plugin_dir_path(__FILE__);
		new Pixelio_Cart_Abandon_Recovery_More_Admin_Job_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
			$this->pixelio_email_service, $this->pixelio_sync_service,$this->profile_scoring_sync_service );
		if (is_admin()) {

			new Pixelio_Cart_Abandon_Recovery_More_Admin_Ajax_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
				$this->pixelio_domain_service,
				$this->pixelio_email_sender, $this->pixelio_config_service, $this->pixelio_auto_service);
			new Pixelio_Cart_Abandon_Recovery_More_Rgpd_Ajax_Controller(
				$this->pixelio_wordpress_wrapper
			);
		}

		if (is_admin() && !wp_doing_ajax()) {
			new Pixelio_Cart_Abandon_Recovery_More_Admin_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
				$this->pixelio_update_service,
				$this->pixelio_update_popup_renderer, $this->pixelio_notice);
		}
		new Pixelio_Cart_Abandon_Recovery_More_Ajax_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
			$this->pixelio_woocommerce_wrapper, $this->pixelio_cart_item, $this->order_service, $this->pixelio_email_coupon,
		$this->user_consent_repo,$this->pixelio_uuid_service,$this->pixelio_cart_model,$this->pixelio_wishlist_service,	$this->pixelio_wishlist_dispatcher,$this->pixelio_wishlist_repository);
		$this->pixelio_session_model = new Pixelio_Cart_Abandon_Recovery_More_Session_Repository($this->pixelio_wordpress_wrapper);
		$event_tracking_controller = new Pixelio_Cart_Abandon_Recovery_More_Ajax_Event_Data_Tracking_Controller(
			$this->pixelio_wordpress_wrapper,
			$this->pixelio_woocommerce_wrapper,
			$this->pixelio_util,
			$this->pixelio_session_model
		);

		$event_tracking_controller->register_hooks();

		if (!is_admin() && !wp_doing_ajax()) {
			new Pixelio_Cart_Abandon_Recovery_More_Cart_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper, $this->pixelio_woocommerce_wrapper,
				$this->pixelio_cart_model, $this->pixelio_cart_item,$this->user_consent_repo,$this->pixelio_uuid_repository);
			new Pixelio_Cart_Abandon_Recovery_More_Frontend_Controller($this->pixelio_util, $this->pixelio_wordpress_wrapper,
				$this->pixelio_woocommerce_wrapper, $this->pixelio_cart_model, $this->pixelio_cart_item,
				$this->pixelio_wishlist_service,
				$this->pixelio_wishlist_repository,
				$this->user_consent_repo,$this->pixelio_uuid_repository);
		}


	}
}
