<?php
/**
 * Displays a bundle metadata.
 *
 * @package EaLab\WP_Deployer
 */

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

/**
 * Requires.
 */
require_once __DIR__ . '/../utility/component-based-object.php';

use EaLab\WP_Deployer\vendor\Ealab\Deployer\Exception\WpDeployException;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\File\KeyValueFile;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\Date;
use EaLab\WP_Deployer\vendor\Ealab\Deployer\Utility\FileManager;

/**
 * Displays a bundle metadata.
 *
 * @param array|null $context The context.
 *
 * @throws WpDeployException Unmanageable errors.
 *
 * @deprecated Not used anymore, because the info is integrated in the new bundle UI.
 */
function deploy_sync_content__bundle_meta( ?array $context = array() ): void {
	$bundle_id = $context['bundle_id'];

	if ( empty( $bundle_id ) ) {
		return;
	}

	$file_manager = deploy_sync_content__get_root_cbo( FileManager::class );

	$bundle_metadata = $file_manager->loadDeploymentBundleMetadata( $bundle_id, 'content.json' ) ?? new KeyValueFile();

	$last_generation_date = $bundle_metadata->getValue( 'last_generation_date' );

	$creation_date_dt = Date::toDateTimeWithFormat( Date::ISO_COMPACT, $bundle_id );

	$last_generation_date_dt = Date::toDateTimeWithFormat( Date::ISO_MILLISECONDS, $last_generation_date );

	$bundle_label = $bundle_metadata->getValue( 'bundle_label', null );

	if ( empty( $bundle_label ) ) {
		$last_generation_date_dt_copy = clone $last_generation_date_dt;

		$bundle_label = $last_generation_date_dt_copy
			->setTimezone( wp_timezone() )
			->format( Date::AS_BUNDLE_LABEL );
	}

	$format_date_for_ui = static function ( DateTime $date ): string {
		// Work on a copy to not modify the original timezone.
		$cloned = clone $date;
		$cloned->setTimezone( wp_timezone() );

		return $cloned->format( Date::getSiteDateTimeFormat() ) . ' (' . wp_timezone_string() . ')';
	};
	?>
	<h2><?php echo esc_html( $bundle_label ); ?></h2>
	<div class="row">
		<label class="col" style="flex: 0 0 200px">
			<strong><?php echo esc_html__( 'Created on', 'deploy-sync-content' ); ?></strong>
		</label>
		<div class="col col-12 col-md"><?php echo esc_html( $format_date_for_ui( $creation_date_dt ) ); ?></div>
	</div>
	<div class="row">
		<label class="col" style="flex: 0 0 200px">
			<strong><?php echo esc_html__( 'Last generated on', 'deploy-sync-content' ); ?></strong>
		</label>
		<div class="col col-12 col-md">
			<?php echo esc_html( $format_date_for_ui( $last_generation_date_dt ) ); ?>
		</div>
	</div>
	<div class="row">
		<label class="col"
			style="flex: 0 0 200px"><strong><?php echo esc_html__( 'Exported by', 'deploy-sync-content' ); ?></strong></label>
		<div
			class="col col-12 col-md"><?php echo esc_html( $bundle_metadata->getValue( 'exported_by', __( 'Unknown issuer', 'deploy-sync-content' ) ) ); ?></div>
	</div>
	<div class="row">
		<label class="col"
			style="flex: 0 0 200px"><strong><?php echo esc_html__( 'Bundle full identifier', 'deploy-sync-content' ); ?></strong></label>
		<div class="col col-12 col-md"><?php echo esc_html( $bundle_id ); ?></div>
	</div>
	<?php
}
