<?php
/**
 * License page: available update summary.
 *
 * @package WP_PowerSuite
 * @since 1.1.0
 *
 * @var WPPS_License_Manager|null $manager
 * @var array<string,mixed>|null  $pending
 */

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

if ( ! isset( $pending ) || ! is_array( $pending ) ) {
	if ( isset( $manager ) && $manager instanceof WPPS_License_Manager ) {
		$pending = $manager->get_page_pending_update();
	} elseif ( class_exists( 'WPPS_License_Updater', false ) ) {
		$pending = WPPS_License_Updater::get_pending_update();
	}
}

if ( ! is_array( $pending ) ) {
	return;
}

$can_download = ! empty( $pending['can_download'] );
$card_class   = $can_download ? 'wpps-license-update-card--ready' : 'wpps-license-update-card--blocked';
?>
<div class="wpps-license-update-card <?php echo esc_attr( $card_class ); ?>">
	<div class="wpps-license-update-card__header">
		<span class="dashicons dashicons-update" aria-hidden="true"></span>
		<strong><?php esc_html_e( 'Plugin update available', 'wp-powersuite' ); ?></strong>
	</div>
	<p class="wpps-license-update-card__versions">
		<?php
		echo esc_html(
			sprintf(
				/* translators: 1: installed version, 2: available version */
				__( 'Installed: %1$s — Available: %2$s', 'wp-powersuite' ),
				$pending['installed_version'],
				$pending['new_version']
			)
		);
		?>
	</p>
	<?php if ( $can_download ) : ?>
		<p class="wpps-license-update-card__message">
			<?php esc_html_e( 'Your license is active. You can install this update from Dashboard → Updates or the Plugins screen.', 'wp-powersuite' ); ?>
		</p>
		<?php if ( ! empty( $pending['updates_url'] ) ) : ?>
			<p class="wpps-license-update-card__actions">
				<a class="button button-primary" href="<?php echo esc_url( (string) $pending['updates_url'] ); ?>">
					<?php esc_html_e( 'Go to Updates', 'wp-powersuite' ); ?>
				</a>
			</p>
		<?php endif; ?>
	<?php else : ?>
		<p class="wpps-license-update-card__message wpps-license-update-card__message--blocked">
			<?php
			echo esc_html(
				(string) ( $pending['block_message'] ?? (
					class_exists( 'WPPS_License_Updater', false )
						? WPPS_License_Updater::get_download_block_message( (string) ( $pending['block_reason'] ?? '' ) )
						: ''
				) )
			);
			?>
		</p>
		<p class="wpps-license-update-card__actions">
			<?php if ( ! empty( $pending['block_reason'] ) && $pending['block_reason'] === 'inactive' && ! empty( $pending['license_url'] ) ) : ?>
				<a class="button button-primary" href="<?php echo esc_url( (string) $pending['license_url'] ); ?>#el_license_key">
					<?php esc_html_e( 'Activate License', 'wp-powersuite' ); ?>
				</a>
			<?php elseif ( ! empty( $pending['license_url'] ) ) : ?>
				<a class="button button-primary" href="<?php echo esc_url( (string) $pending['license_url'] ); ?>">
					<?php esc_html_e( 'Manage License', 'wp-powersuite' ); ?>
				</a>
			<?php endif; ?>
		</p>
	<?php endif; ?>
</div>
