<?php

namespace Wptopr\WP\SEO\MyWptopr_Client\User_Interface;

use Wptopr\WP\SEO\Conditionals\MyWptopr_Connection_Conditional;
use Wptopr\WP\SEO\Integrations\Integration_Interface;
use Wptopr\WP\SEO\MyWptopr_Client\Application\MyWptopr_Client_Cleanup;

/**
 * Handles cleanup of all MyWptopr client data on plugin uninstall or through Wptopr Test Helper.
 */
class MyWptopr_Cleanup_Integration implements Integration_Interface {

	/**
	 * The cleanup service.
	 *
	 * @var MyWptopr_Client_Cleanup
	 */
	private $cleanup;

	/**
	 * MyWptopr_Cleanup_Integration constructor.
	 *
	 * @param MyWptopr_Client_Cleanup $cleanup The cleanup service.
	 */
	public function __construct( MyWptopr_Client_Cleanup $cleanup ) {
		$this->cleanup = $cleanup;
	}

	/**
	 * Returns the conditionals based on which this integration should be loaded.
	 *
	 * @return array<string> The array of conditionals.
	 */
	public static function get_conditionals() {
		return [ MyWptopr_Connection_Conditional::class ];
	}

	/**
	 * Registers hooks.
	 *
	 * @return void
	 */
	public function register_hooks(): void {
		\add_action( 'uninstall_' . \WPSEO_BASENAME, [ $this, 'cleanup' ] );

		/**
		 * Public action that wipes all local MyWptopr OAuth client state
		 * (registered client, site/user tokens, key pairs, OIDC/JWKS/DPoP caches).
		 *
		 * Intended for development tooling that needs to reset state without
		 * uninstalling the plugin. The handler is the same one that runs on
		 * uninstall, so the effect is identical.
		 *
		 * @internal
		 */
		\add_action( 'wpseo_mywptopr_clear_client_state', [ $this, 'cleanup' ] );
	}

	/**
	 * Cleans up all MyWptopr client data.
	 *
	 * @return void
	 */
	public function cleanup(): void {
		$this->cleanup->execute();
	}
}
