<?php
/**
 * Step 1: Thumbnail Management.
 *
 * @package CoffeeWP_Media_Cleanup
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template variables are scoped to the include() call from the render method, not actually global.

$sizes         = CWPM\Thumbnail\Sizes::get_all();
$total         = count( $sizes );
$enabled       = count(
	array_filter(
		$sizes,
		function ( $s ) {
			return $s['enabled'];
		}
	)
);
$source_badges = array(
	'core'   => 'badge-info',
	'theme'  => 'badge-secondary',
	'plugin' => 'badge-warning',
);
?>
<section class="cwpm-step" id="cwpm-step-thumbnails" data-step="1">
	<h4 class="cwpm-step__title">
		<span class="badge badge-primary">1</span>
		<?php esc_html_e( 'Thumbnail Sizes', 'coffeewp-media-cleanup' ); ?>
	</h4>
	<p class="cwpm-step__desc">
		<?php
		printf(
			/* translators: %1$d: enabled count, %2$d: total count */
			esc_html__( '%1$d of %2$d sizes enabled. Toggle off sizes you don\'t need to prevent WordPress from generating them.', 'coffeewp-media-cleanup' ),
			(int) $enabled,
			(int) $total
		);
		?>
	</p>

	<div id="cwpm-thumbnails-content">
		<div class="cwpm-table-wrap">
			<table class="table table--heading-primary" id="cwpm-thumbnails-table">
				<thead>
					<tr>
						<th class="cwpm-col-toggle"><?php esc_html_e( 'Enabled', 'coffeewp-media-cleanup' ); ?></th>
						<th><?php esc_html_e( 'Name', 'coffeewp-media-cleanup' ); ?></th>
						<th><?php esc_html_e( 'Dimensions', 'coffeewp-media-cleanup' ); ?></th>
						<th><?php esc_html_e( 'Crop', 'coffeewp-media-cleanup' ); ?></th>
						<th><?php esc_html_e( 'Source', 'coffeewp-media-cleanup' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $sizes as $name => $size ) : ?>
						<tr data-size="<?php echo esc_attr( $name ); ?>">
							<td class="cwpm-col-toggle">
								<label class="switch">
									<input
										type="checkbox"
										class="switch__input cwpm-size-toggle"
										data-size="<?php echo esc_attr( $name ); ?>"
										<?php checked( $size['enabled'] ); ?>
									/>
									<span class="switch__track" aria-hidden="true"></span>
								</label>
							</td>
							<td>
								<strong><?php echo esc_html( CWPM\Thumbnail\Sizes::format_name( $name ) ); ?></strong>
								<code class="cwpm-size-slug"><?php echo esc_html( $name ); ?></code>
							</td>
							<td><?php echo wp_kses( CWPM\Thumbnail\Sizes::format_dimensions( $size['width'], $size['height'] ), array() ); ?></td>
							<td>
								<?php if ( $size['crop'] ) : ?>
									<span class="icon icon--xs text-success" aria-label="<?php esc_attr_e( 'Yes', 'coffeewp-media-cleanup' ); ?>"><i class="fa-solid fa-check"></i></span>
								<?php else : ?>
									<span class="icon icon--xs text-muted" aria-label="<?php esc_attr_e( 'No', 'coffeewp-media-cleanup' ); ?>"><i class="fa-solid fa-minus"></i></span>
								<?php endif; ?>
							</td>
							<td>
								<?php
								$badge_class = isset( $source_badges[ $size['source'] ] ) ? $source_badges[ $size['source'] ] : 'badge-neutral';
								?>
								<span class="badge badge-sm <?php echo esc_attr( $badge_class ); ?>">
									<?php echo esc_html( ucfirst( $size['source'] ) ); ?>
								</span>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>

		<div class="cwpm-step__footer">
			<button type="button" class="btn btn--primary cwpm-next-step" data-next="2">
				<?php esc_html_e( 'Next: Scan Media', 'coffeewp-media-cleanup' ); ?>
				<span class="icon icon--xs" aria-hidden="true"><i class="fa-solid fa-arrow-right"></i></span>
			</button>
		</div>
	</div>
</section>
