<?php
/**
 * Dashboard template.
 *
 * @package NaveenCodesImageOptimizer
 */

defined( 'ABSPATH' ) || exit;

(
	static function ( $banner, $health, $registration, $stats, $queue_counts, $settings, $recent_items ) {
		$ultra_image_optimizer_saved_total = size_format( (int) ( $stats['total_saved_bytes'] ?? 0 ), 2 );
		$ultra_image_optimizer_avg         = number_format_i18n( (float) ( $stats['average_compression'] ?? 0 ), 2 ) . '%';
		?>
<div class="wrap naveencodes-wrap">
	<?php include NAVEENCODES_DIR . 'templates/admin-banner.php'; ?>
	<div class="naveencodes-header">
		<div>
			<h1><?php esc_html_e( 'NaveenCodes Image Optimizer', 'naveencodes-image-optimizer' ); ?></h1>
			<p class="naveencodes-subtitle"><?php esc_html_e( 'Track savings, review optimization activity, and keep your media library lean from one dashboard.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-header-actions">
			<span class="naveencodes-badge <?php echo esc_attr( $health['badge_class'] ); ?>">
				<?php esc_html_e( 'Health', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $health['label'] ); ?>
			</span>
			<span class="naveencodes-badge <?php echo esc_attr( $registration['badge_class'] ); ?>">
				<?php esc_html_e( 'Registration', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $registration['status_label'] ); ?>
			</span>
			<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'admin.php?page=naveencodes-settings' ) ); ?>">
				<?php esc_html_e( 'Open Settings', 'naveencodes-image-optimizer' ); ?>
			</a>
		</div>
	</div>

	<div class="naveencodes-grid naveencodes-grid--stats" aria-label="<?php esc_attr_e( 'Plugin analytics', 'naveencodes-image-optimizer' ); ?>">
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Total Images Optimized', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value" data-naveencodes-stat="total_optimized"><?php echo esc_html( number_format_i18n( (int) $stats['total_optimized'] ) ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Successful media items processed by the plugin.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Total Space Saved', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value" data-naveencodes-stat-format="bytes" data-naveencodes-stat="total_saved_bytes"><?php echo esc_html( $ultra_image_optimizer_saved_total ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Combined reduction across all successful optimizations.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Average Compression', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value" data-naveencodes-stat-format="percent" data-naveencodes-stat="average_compression"><?php echo esc_html( $ultra_image_optimizer_avg ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Average savings compared with original file size.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Failed Optimizations', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value" data-naveencodes-stat="failed_optimizations"><?php echo esc_html( number_format_i18n( (int) $stats['failed_optimizations'] ) ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Items that need attention or a retry.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Queued Images', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value" data-naveencodes-queue-total><?php echo esc_html( number_format_i18n( (int) $queue_counts['pending'] + (int) $queue_counts['processing'] ) ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Pending and currently processing items.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
		<div class="naveencodes-card">
			<p class="naveencodes-card-label"><?php esc_html_e( 'Current Batch Size', 'naveencodes-image-optimizer' ); ?></p>
			<p class="naveencodes-card-value"><?php echo esc_html( number_format_i18n( (int) $settings['batch_size'] ) ); ?></p>
			<p class="naveencodes-card-note"><?php esc_html_e( 'Images processed in each batch cycle.', 'naveencodes-image-optimizer' ); ?></p>
		</div>
	</div>

	<div class="naveencodes-panel naveencodes-panel--hero" data-naveencodes-live-progress>
		<div class="naveencodes-panel-head">
			<div>
				<h2><?php esc_html_e( 'Optimization Overview', 'naveencodes-image-optimizer' ); ?></h2>
				<p class="naveencodes-help"><?php esc_html_e( 'Use bulk optimization for large libraries, or optimize individual images directly from the Media Library.', 'naveencodes-image-optimizer' ); ?></p>
			</div>
			<div class="naveencodes-actions">
				<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=naveencodes-optimize-media' ) ); ?>"><?php esc_html_e( 'Open Optimize Media', 'naveencodes-image-optimizer' ); ?></a>
				<a class="button button-secondary" href="<?php echo esc_url( admin_url( 'upload.php' ) ); ?>"><?php esc_html_e( 'Open Media Library', 'naveencodes-image-optimizer' ); ?></a>
			</div>
		</div>

		<ul class="naveencodes-status-list">
			<li><span class="naveencodes-badge naveencodes-badge--info" data-naveencodes-count-pending><?php esc_html_e( 'Pending', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( number_format_i18n( (int) $queue_counts['pending'] ) ); ?></span></li>
			<li><span class="naveencodes-badge naveencodes-badge--warning" data-naveencodes-count-processing><?php esc_html_e( 'Processing', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( number_format_i18n( (int) $queue_counts['processing'] ) ); ?></span></li>
			<li><span class="naveencodes-badge naveencodes-badge--success" data-naveencodes-count-completed><?php esc_html_e( 'Completed', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( number_format_i18n( (int) $queue_counts['completed'] ) ); ?></span></li>
			<li><span class="naveencodes-badge naveencodes-badge--danger" data-naveencodes-count-failed><?php esc_html_e( 'Failed', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( number_format_i18n( (int) $queue_counts['failed'] ) ); ?></span></li>
		</ul>

		<div class="naveencodes-info-grid">
			<div class="naveencodes-info-card">
				<p class="naveencodes-card-label"><?php esc_html_e( 'Website Registration', 'naveencodes-image-optimizer' ); ?></p>
				<p class="naveencodes-info-value"><?php echo esc_html( $registration['status_label'] ); ?></p>
				<p class="naveencodes-help"><?php echo esc_html( $registration['message'] ); ?></p>
			</div>
			<div class="naveencodes-info-card">
				<p class="naveencodes-card-label"><?php esc_html_e( 'Service Health', 'naveencodes-image-optimizer' ); ?></p>
				<p class="naveencodes-info-value"><?php echo esc_html( $health['label'] ); ?></p>
				<p class="naveencodes-help"><?php echo esc_html( $health['message'] ); ?></p>
			</div>
			<div class="naveencodes-info-card">
				<p class="naveencodes-card-label"><?php esc_html_e( 'Compression Profile', 'naveencodes-image-optimizer' ); ?></p>
				<p class="naveencodes-info-value"><?php echo esc_html( number_format_i18n( (int) $settings['quality'] ) ); ?> <?php esc_html_e( 'quality', 'naveencodes-image-optimizer' ); ?> / <?php echo esc_html( ucfirst( (string) $settings['mode'] ) ); ?> <?php esc_html_e( 'mode', 'naveencodes-image-optimizer' ); ?></p>
				<p class="naveencodes-help"><?php esc_html_e( 'Max width', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( number_format_i18n( (int) $settings['max_width'] ) ); ?> px</p>
			</div>
		</div>
	</div>

	<div class="naveencodes-panel">
		<div class="naveencodes-panel-head">
			<div>
				<h2><?php esc_html_e( 'Recently Optimized Images', 'naveencodes-image-optimizer' ); ?></h2>
				<p class="naveencodes-help"><?php esc_html_e( 'Review the latest optimized attachments and compare their before and after file sizes.', 'naveencodes-image-optimizer' ); ?></p>
			</div>
		</div>

		<?php if ( empty( $recent_items ) ) : ?>
			<div class="naveencodes-empty-state">
				<p><?php esc_html_e( 'No optimized images yet. Start a bulk run or optimize an image from the Media Library.', 'naveencodes-image-optimizer' ); ?></p>
			</div>
		<?php else : ?>
			<div class="naveencodes-media-list">
				<?php foreach ( $recent_items as $ultra_image_optimizer_item ) : ?>
					<div class="naveencodes-media-item">
						<div class="naveencodes-media-thumb"><?php echo wp_kses_post( $ultra_image_optimizer_item['thumbnail'] ); ?></div>
						<div class="naveencodes-media-content">
							<div class="naveencodes-media-topline">
								<strong><?php echo esc_html( $ultra_image_optimizer_item['title'] ?: $ultra_image_optimizer_item['filename'] ); ?></strong>
								<span class="naveencodes-badge <?php echo esc_attr( $ultra_image_optimizer_item['status_class'] ); ?>"><?php echo esc_html( $ultra_image_optimizer_item['status_label'] ); ?></span>
							</div>
							<p class="naveencodes-media-filename"><?php echo esc_html( $ultra_image_optimizer_item['filename'] ); ?></p>
							<div class="naveencodes-metric-row">
								<span><?php esc_html_e( 'Before', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $ultra_image_optimizer_item['before_label'] ); ?></span>
								<span><?php esc_html_e( 'After', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $ultra_image_optimizer_item['after_label'] ); ?></span>
								<span><?php esc_html_e( 'Saved', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $ultra_image_optimizer_item['saved_label'] ); ?></span>
								<span><?php esc_html_e( 'Compression', 'naveencodes-image-optimizer' ); ?>: <?php echo esc_html( $ultra_image_optimizer_item['compression_label'] ); ?></span>
							</div>
						</div>
						<div class="naveencodes-media-actions">
							<?php if ( ! empty( $ultra_image_optimizer_item['edit_url'] ) ) : ?>
								<a class="button button-secondary" href="<?php echo esc_url( $ultra_image_optimizer_item['edit_url'] ); ?>"><?php esc_html_e( 'View Attachment', 'naveencodes-image-optimizer' ); ?></a>
							<?php endif; ?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>
</div>
		<?php
	}
)( $banner, $health, $registration, $stats, $queue_counts, $settings, $recent_items );
