<?php
/**
 * WP-CLI commands for WP PowerSuite licensing.
 *
 * @package WP_PowerSuite
 * @since 1.1.0
 */

if ( ! defined( 'ABSPATH' ) && ! ( defined( 'WP_CLI' ) && WP_CLI ) ) {
	exit;
}

/**
 * @since 1.1.0
 */
final class WPPS_License_CLI {

	/**
	 * Register WP-CLI commands when available.
	 */
	public static function register(): void {
		if ( ! class_exists( 'WP_CLI', false ) ) {
			return;
		}

		\WP_CLI::add_command( 'powersuite license', self::class );
	}

	/**
	 * Show license status summary.
	 *
	 * ## EXAMPLES
	 *
	 *     wp powersuite license status
	 *
	 * @when after_wp_load
	 */
	public function status( array $args, array $assoc_args ): void {
		unset( $args, $assoc_args );

		if ( ! class_exists( 'WPPS_License_Manager', false ) ) {
			\WP_CLI::error( 'WP PowerSuite licensing is not available in this build.' );
		}

		$manager = WPPS_License_Manager::get_instance();
		$manager->init();
		$data    = $manager->get_status_data();

		$row = [
			'status'          => (string) ( $data['status'] ?? 'unknown' ),
			'premium_allowed' => function_exists( 'wp_powersuite_is_license_valid' ) && wp_powersuite_is_license_valid() ? 'yes' : 'no',
			'updates_allowed' => function_exists( 'wp_powersuite_is_license_current' ) && wp_powersuite_is_license_current() ? 'yes' : 'no',
			'email'           => $manager->get_license_email(),
			'last_checked'    => ! empty( $data['checked_at'] ) ? gmdate( 'Y-m-d H:i:s', (int) $data['checked_at'] ) . ' UTC' : 'never',
		];

		if ( class_exists( 'WPPS_License_Updater', false ) ) {
			$pending = WPPS_License_Updater::get_pending_update();
			$row['pending_update'] = is_array( $pending )
				? (string) $pending['new_version'] . ' (installed ' . (string) $pending['installed_version'] . ')'
				: 'none';
		}

		\WP_CLI\Utils\format_items( 'table', [ $row ], array_keys( $row ) );
	}

	/**
	 * Refresh license status from the license server.
	 *
	 * ## EXAMPLES
	 *
	 *     wp powersuite license refresh
	 *
	 * @when after_wp_load
	 */
	public function refresh( array $args, array $assoc_args ): void {
		unset( $args, $assoc_args );

		if ( ! class_exists( 'WPPS_License_Manager', false ) ) {
			\WP_CLI::error( 'WP PowerSuite licensing is not available in this build.' );
		}

		$manager = WPPS_License_Manager::get_instance();
		$manager->init();
		$manager->sync_for_admin_page( true );

		$data = $manager->get_status_data();
		\WP_CLI::success(
			sprintf(
				'License status: %s',
				(string) ( $data['status'] ?? 'unknown' )
			)
		);
	}

	/**
	 * Clear Elite Licenser and WordPress plugin update caches.
	 *
	 * ## EXAMPLES
	 *
	 *     wp powersuite license clear-update-cache
	 *
	 * @when after_wp_load
	 */
	public function clear_update_cache( array $args, array $assoc_args ): void {
		unset( $args, $assoc_args );

		if ( class_exists( 'WPPS_License_Updater', false ) ) {
			WPPS_License_Updater::clear_update_caches();
			\WP_CLI::success( 'WP PowerSuite update caches cleared.' );
			return;
		}

		delete_site_transient( 'update_plugins' );
		\WP_CLI::success( 'WordPress update_plugins transient cleared.' );
	}
}
