<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Services;


use PixelioCartAbandonRecoveryMore\Admin\Exception\Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Exception;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

/**
 * Service responsible for handling the toggle of the IA auto mode
 * from an incoming POST request.
 *
 * This service:
 * - Extracts the "enabled" value from POST
 * - Persists the IA setup mode option
 * - Returns a formatted response for AJAX usage
 *
 * Dependencies are injected to allow unit testing without
 * loading WordPress.
 */
class Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Service
{
	/**
	 * WordPress abstraction layer (POST, sanitize, esc_html, etc).
	 *
	 * @var Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper
	 */
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;

	/**
	 * Pixelio utility service (options + business logic).
	 *
	 * @var Pixelio_Cart_Abandon_Recovery_More_Util
	 */
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;

	/**
	 * Constructor.
	 *
	 * @param Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper Wrapper for WordPress functions.
	 * @param Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util      Utility service for Pixelio options.
	 */
	public function __construct(Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper,Pixelio_Cart_Abandon_Recovery_More_Util  $pixelio_util)
	{
		$this->pixelio_wordpress_wrapper = $wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
	}

	/**
	 * Toggle IA auto mode from POST request.
	 *
	 * Workflow:
	 * 1. Extract "enabled" from POST
	 * 2. Persist option
	 * 3. Build response array
	 *
	 * @return array{
	 *     message: string,
	 *     enabled: bool
	 * }
	 *
	 * @throws Pixelio_Cart_Abandon_Recovery_More_Auto_Mode_Exception
	 *         When the "enabled" field is missing.
	 */
	public function pixelio_toggle_from_request($enabled): array
	{

		$this->pixelio_persist_auto_mode($enabled);

		return $this->pixelio_build_response();
	}



	/**
	 * Persist IA setup mode option.
	 *
	 * @param bool $enabled Whether auto mode should be enabled.
	 *
	 * @return void
	 */
	private function pixelio_persist_auto_mode($enabled): void
	{
		$this->pixelio_util
			->pixelio_cart_abandon_recovery_more_add_or_update_option(
				'ia-setup-mode',
				$enabled ? 'on' : 'off'
			);
	}

	/**
	 * Build the response returned to AJAX.
	 *
	 * @return array{
	 *     message: string,
	 *     enabled: bool
	 * }
	 */
	private function pixelio_build_response(): array
	{
		return array(
			'message' => $this->pixelio_wordpress_wrapper->pixelio_esc_html(
				$this->pixelio_util
					->pixelio_cart_abandon_recovery_more_get_ia_label_checkbox()
			),
			'enabled' => $this->pixelio_util
				->pixelio_cart_abandon_recovery_more_dependency_is_checked('ia-setup-mode'),
		);
	}

}
