<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Connector;

/**
 * Silent Upgrader Skin for the Hub Connector module.
 *
 * Suppresses all Plugin_Upgrader output so it does not corrupt the JSON REST response.
 * Ported from StrykeHub_Silent_Upgrader_Skin (stryke-connector v1.0.0).
 *
 * LOAD ORDER: This class MUST be loaded AFTER wp-admin/includes/class-wp-upgrader.php
 * because it extends WP_Upgrader_Skin. ApiManager handles this requirement.
 *
 * @package StrykeWP\Modules\Connector
 */
class SilentUpgraderSkin extends \WP_Upgrader_Skin {

	/** @var string[] Captured error messages. */
	private array $error_messages = array();

	/** @var string[] Captured feedback messages (for debugging). */
	private array $feedback_messages = array();

	// -----------------------------------------------------------------------
	// Suppressed Output Methods
	// -----------------------------------------------------------------------

	/**
	 * Capture feedback instead of echoing it.
	 *
	 * @param string|\WP_Error $string Message key or WP_Error.
	 * @param mixed            ...$args Additional format args (unused).
	 * @return void
	 */
	public function feedback( $string, ...$args ): void {
		if ( is_wp_error( $string ) ) {
			foreach ( $string->get_error_messages() as $msg ) {
				$this->error_messages[] = wp_strip_all_tags( $msg );
			}
			return;
		}

		if ( isset( $this->upgrader->strings[ $string ] ) ) {
			$string = $this->upgrader->strings[ $string ];
		}

		if ( is_string( $string ) && '' !== $string ) {
			$this->feedback_messages[] = wp_strip_all_tags( $string );
		}
	}

	/** @return void */
	public function header(): void {}

	/** @return void */
	public function footer(): void {}

	/** @return void */
	public function before(): void {}

	/** @return void */
	public function after(): void {}

	/**
	 * Capture errors instead of echoing them.
	 *
	 * @param string|\WP_Error $errors The error(s) to capture.
	 * @return void
	 */
	public function error( $errors ): void {
		if ( is_wp_error( $errors ) ) {
			foreach ( $errors->get_error_messages() as $message ) {
				$this->error_messages[] = wp_strip_all_tags( $message );
			}
		} elseif ( is_string( $errors ) && '' !== $errors ) {
			$this->error_messages[] = wp_strip_all_tags( $errors );
		}
	}

	// -----------------------------------------------------------------------
	// Result Accessors
	// -----------------------------------------------------------------------

	/**
	 * Returns all captured error messages from the upgrade process.
	 *
	 * @return string[]
	 */
	public function get_error_messages(): array {
		return $this->error_messages;
	}

	/**
	 * Returns all captured feedback messages (primarily for debugging).
	 *
	 * @return string[]
	 */
	public function get_feedback_messages(): array {
		return $this->feedback_messages;
	}

	/**
	 * Returns true if any errors were captured during the upgrade.
	 *
	 * @return bool
	 */
	public function has_errors(): bool {
		return ! empty( $this->error_messages );
	}
}
