<?php
/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Job_Controller
 *
 * Handles scheduled tasks (cron jobs) for the Pixelio Cart Abandonment plugin,
 * including syncing tables, sending abandoned cart emails, and review request emails.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Controllers
 * @since 0.1
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Controllers;


use PixelioCartAbandonRecoveryMore\Admin\Services\Pixelio_Cart_Abandon_Recovery_More_Email_Builder_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\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Controllers\Pixelio_Cart_Abandon_Recovery_More_General_Controller;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Repository;


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

/**
 * Class Pixelio_Cart_Abandon_Recovery_More_Job_Controller
 *
 * Handles scheduled tasks (cron jobs) for the Pixelio Cart Abandonment plugin,
 * including syncing tables, sending abandoned cart emails, and review request emails.
 *
 * @package PixelioCartAbandonRecoveryMore\Admin\Controllers
 * @since 0.1
 */
class Pixelio_Cart_Abandon_Recovery_More_Admin_Job_Controller extends Pixelio_Cart_Abandon_Recovery_More_General_Controller
{

	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Sync_Service $pixelio_sync_service;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Builder_Service $pixelio_email_service;

	private Pixelio_Cart_Abandon_Recovery_More_Segmentation_Customer_Profile_Sync_Service $pixelio_profile_service;

	/**
	 * Constructor.
	 *
	 * Sets up cron schedules and triggers syncing.
	 */
	public function __construct($pixelio_util, $pixelio_wordpress_wrapper, $pixelio_email_service, $pixelio_sync_service,$pixelio_profile_service)
	{
		$this->pixelio_util = $pixelio_util;
		$this->pixelio_wordpress_wrapper = $pixelio_wordpress_wrapper;
		$this->pixelio_email_service = $pixelio_email_service;
		$this->pixelio_sync_service = $pixelio_sync_service;
		$this->pixelio_profile_service = $pixelio_profile_service;
		parent::__construct($pixelio_wordpress_wrapper, $pixelio_util);
		$this->pixelio_wordpress_wrapper->pixelio_add_filter('cron_schedules', [$this, 'pixelio_add_five_minutes_schedule']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('wp', array($this, 'pixelio_cart_abandon_recovery_more_pixelio_custom_cron_schedule'));
		$this->pixelio_wordpress_wrapper->pixelio_add_action('pixelio_send_emails', [$this, 'pixelio_send_emails']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('pixelio_send_review_emails', [$this, 'pixelio_send_review_emails']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('pixelio_mark_cart_as_abandoned', [$this, 'pixelio_mark_cart_as_abandoned']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('pixelio_send_scoring', [$this, 'pixelio_send_scoring']);
	}




	/**
	 * Add custom schedule of 5 minutes for WordPress cron.
	 *
	 * @param array $schedules Existing schedules.
	 * @return array Modified schedules.
	 */
	public function pixelio_add_five_minutes_schedule($schedules): array
	{
		$schedules['five_minutes'] = array(
			'interval' => 300,
			'display' => esc_html('Every 5 Minutes'),
		);
		return $schedules;
	}

	/**
	 * Schedule all plugin cron jobs if not already scheduled.
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_pixelio_custom_cron_schedule(): void
	{
		if (!$this->pixelio_wordpress_wrapper->pixelio_wp_next_scheduled('pixelio_send_emails')) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_schedule_event(time(), 'five_minutes', 'pixelio_send_emails');
		}

		if (!$this->pixelio_wordpress_wrapper->pixelio_wp_next_scheduled('pixelio_send_emails')) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_schedule_event(time(), 'five_minutes', 'pixelio_mark_cart_as_abandoned');
		}

		if (!$this->pixelio_wordpress_wrapper->pixelio_wp_next_scheduled('pixelio_send_review_emails')) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_schedule_event(time(), 'five_minutes', 'pixelio_send_review_emails');
		}

		if (!$this->pixelio_wordpress_wrapper->pixelio_wp_next_scheduled('pixelio_send_scoring')) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_schedule_event(time(), 'five_minutes', 'pixelio_send_scoring');
		}
	}




	public function pixelio_send_scoring()
	{
		$this->pixelio_profile_service->pixelio_cart_abandon_recovery_more_sync();

	}

	public function pixelio_send_emails()
	{

		return $this->pixelio_send_emails_generic([
			'type' => 'abandon',
			'dependency' => 'pixelio-cart-abandon-recovery-more-abandon-checkbox',
			'endpoint' => 'send_email',
			'update_endpoint' => 'update_status',
			'method' => 'POST',
			'type_of_email' => 1,
			'with_discount' => 0,
			'status' => "5",
			'coupon_dependency' => 'pixelio-cart-abandon-recovery-more-use-promo-code-checkbox',
			'coupon_value_dependency' => 'pixelio-cart-abandon-recovery-more-use-promo-code-percent',
			'status_builder' => function ($data, $link, $unsub) {
				return [
					'cart_id' => $data['cart_id'] ?? 0,
					'consent' => 1,
					'token_for_recover' => $link,
					'unsubscribed_link' => $unsub,
					'email_id' => $data['id'] ?? 0, // template id, not user email id
				];
			},
		]);
	}

	/**
	 * Send abandoned cart emails.
	 *
	 * @return array
	 */

	public function pixelio_send_emails_generic(array $config): ?array
	{
		$this->pixelio_sync_service->pixelio_sync_tables_cron();
		$status_data = $this->pixelio_email_service->pixelio_process_emails($config);

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


		return $status_data;
	}


	/**
	 * Send review request emails to customers who have placed orders.
	 *
	 * This method checks if the "request review" feature is enabled. If so, it retrieves
	 * the list of review emails to send via the plugin's API, generates personalized emails
	 * for each customer, optionally creates a discount coupon, logs the sent emails,
	 * and updates the review status via a remote API call.
	 *
	 * The email includes:
	 * - Review request content
	 * - A link to leave a review on the purchased product
	 * - An optional coupon code
	 * - An unsubscription link
	 *
	 * @return array
	 */
	public function pixelio_send_review_emails(): ?array
	{
		return $this->pixelio_send_emails_generic([
			'type' => 'review',
			'dependency' => 'pixelio-cart-abandon-recovery-more-request-review-checkbox',
			'endpoint' => 'send_review_email',
			'update_endpoint' => 'update_status',
			'method' => 'POST',
			'type_of_email' => 2,
			'with_discount' => 1,
			'status' => "4,22",
			'coupon_dependency' => 'pixelio-cart-abandon-recovery-more-request-review-with-promo-checkbox',
			'coupon_value_dependency' => 'pixelio-cart-abandon-recovery-more-request-review-discount',
			'status_builder' => function ($data, $link, $unsub) {
				return [
					'cart_id' => $data['cart_id'] ?? 0,
					'link_to_review' => $link,
					'unsubscribed_link' => $unsub,
				];
			},
		]);
	}

	public function pixelio_mark_cart_as_abandoned()
	{
		$cart = new Pixelio_Cart_Abandon_Recovery_More_Cart_Repository($this->pixelio_wordpress_wrapper);
		$cart->pixelio_mark_cart_as_abandoned_carts();
		$cart->pixelio_mark_cart_as_fully_abandoned_carts();
	}

	/**
	 * Remove all scheduled cron events related to the Pixelio Cart Abandonment plugin.
	 *
	 * This method unschedules the following recurring tasks if they exist:
	 * - Checking abandoned carts ('pixelio_check_abandoned_cart')
	 * - Synchronizing tables with the remote API ('pixelio_sync_tables_cron')
	 * - Sending abandoned cart reminder emails ('pixelio_send_emails')
	 * - Sending review request emails ('pixelio_send_review_emails')
	 *
	 * @return void
	 */
	public function pixelio_cart_abandon_recovery_more_remove_schedule(): void
	{
		$timestamp = wp_next_scheduled('pixelio_check_abandoned_cart');
		wp_unschedule_event($timestamp, 'pixelio_check_abandoned_cart');
		$timestamp = wp_next_scheduled('pixelio_sync_tables_cron');
		wp_unschedule_event($timestamp, 'pixelio_sync_tables_cron');
		$timestamp = wp_next_scheduled('pixelio_send_emails');
		wp_unschedule_event($timestamp, 'pixelio_send_emails');
		$timestamp = wp_next_scheduled('pixelio_send_review_emails');
		wp_unschedule_event($timestamp, 'pixelio_send_review_emails');
	}


}
