<?php
/**
 * Main class for deployment management.
 *
 * @package Ealab\Deployer
 */

namespace Ealab\Deployer;

use Dakwamine\Component\ComponentBasedObject;
use Dakwamine\Component\RootDependencyDefinition;
use Ealab\Deployer\Exception\WpDeployException;
use Ealab\Deployer\Tracking\TrackingManager;
use Ealab\Deployer\Utility\CommonDataStorage;
use Ealab\Deployer\Utility\FileManager;
use Ealab\Deployer\Utility\Settings;
use Ealab\Deployer\Utility\WpdbWrapper;

/**
 * Main class for deployment management.
 */
class DeploymentManager extends ComponentBasedObject {

	/**
	 * The common data storage.
	 *
	 * @var CommonDataStorage|null
	 */
	private ?CommonDataStorage $common_data_storage = null;

	/**
	 * The file manager.
	 *
	 * @var FileManager|null
	 */
	private ?FileManager $file_manager = null;

	/**
	 * Settings helper.
	 *
	 * @var Settings|null
	 */
	private ?Settings $settings = null;

	/**
	 * The tracking manager.
	 *
	 * @var TrackingManager|null
	 */
	private ?TrackingManager $tracking_manager = null;

	/**
	 * {@inheritDoc}
	 */
	public function getDependencyDefinitions(): array {
		$definitions   = parent::getDependencyDefinitions();
		$definitions[] = new RootDependencyDefinition( CommonDataStorage::class, $this->common_data_storage );
		$definitions[] = new RootDependencyDefinition( FileManager::class, $this->file_manager );
		$definitions[] = new RootDependencyDefinition( Settings::class, $this->settings );
		$definitions[] = new RootDependencyDefinition( TrackingManager::class, $this->tracking_manager );

		return $definitions;
	}

	/**
	 * Call this on plugin activation.
	 */
	public function onPluginActivation(): void {
		$this->settings->initializeSettings();
		$this->common_data_storage->prepareTable();
		TrackingManager::createTrackQueueTable();

		// Note: do not redirect on activation; this would prevent proper activation of the plugins.
		// TODO: initialiser quand c'est bon.
		//$this->tracking_manager->initializeTracking();
		// Welcome message.
		$real_message = _dgettext( 'deployer-core', 'Thank you for using <em>Deploy & Sync Content</em>.' );

		$dashboard_page_url = admin_url(
			add_query_arg(
				array(
					'tab' => 'dashboard',
				),
				DEPLOY_SYNC_CONTENT__BASIC_URI
			)
		);

		$real_message .= ' ' . sprintf( _dgettext( 'deployer-core', 'Please go to the <a href="%s"><em>Dashboard</em> page</a> to start using it.' ), esc_attr( $dashboard_page_url ) );

		$message = <<<WELCOME
				<div class="notice notice-info is-dismissible"><p>
				$real_message
				</p></div>
				WELCOME;

		set_transient( 'deploy_sync_content__activation_message', $message, 100 );
	}

	/**
	 * Call this on plugin deactivation.
	 */
	public function onPluginDeactivation(): void {
		global $wpdb;

		if ( $wpdb instanceof WpdbWrapper ) {
			// This will prevent any tracking to be done during the deactivation.
			$wpdb->unregisterWrapper();
		}

		// Cancel all current tracking actions.
		as_unschedule_all_actions( 'deploy_sync_content/track_item' );

		// Cancel all standard batches.
		as_unschedule_all_actions( 'deploy_sync_content/process_batch_step' );

		// TODO: provide an option to keep the tracking data.
		$this->file_manager->clearExportedData();

		try {
			$this->tracking_manager->removeAllTracking();
		} catch ( WpDeployException $e ) {
			// There was a failure on tracker removal. But let's not block the deactivation process.
		}

		TrackingManager::dropTrackQueueTable();
		$this->common_data_storage->removeTable();
	}

	/**
	 * Upgrades the data used by Deploy & Sync Content.
	 */
	public static function upgradePluginData(): void {
		TrackingManager::upgradePluginData();
	}
}
