<?php
/**
 * Step 1: Settings (thumbnails, batch sizes).
 *
 * @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',
	'unknown' => 'badge-neutral',
);

$batch_size = (int) get_option( 'cwpm_batch_size', 50 );
$batch_size = $batch_size >= 1 && $batch_size <= 500 ? $batch_size : 50;

?>
<section class="cwpm-step" id="cwpm-step-settings" data-step="1">
	<h4 class="cwpm-step__title">
		<span class="badge badge-primary">1</span>
		<?php esc_html_e( 'Scan Setup', 'coffeewp-media-cleanup' ); ?>
	</h4>
	<p class="cwpm-step__desc">
		<?php esc_html_e( 'Review detected plugin compatibility, choose batch size, and pick which thumbnail sizes to scan.', 'coffeewp-media-cleanup' ); ?>
	</p>

	<div id="cwpm-settings-content">

		<!-- Plugin Compatibility -->
		<div class="cwpm-settings-section" id="cwpm-compat-notice" style="display:none;" aria-live="polite">
			<h5 class="cwpm-settings-section__title">
				<?php esc_html_e( 'Plugin Compatibility', 'coffeewp-media-cleanup' ); ?>
			</h5>
			<div id="cwpm-compat-notice-grid"></div>
		</div>

		<!-- Batch size (used for all batch requests) -->
		<div class="cwpm-settings-section">
			<h5 class="cwpm-settings-section__title"><?php esc_html_e( 'Batch size', 'coffeewp-media-cleanup' ); ?></h5>
			<div class="form__group">
				<label for="cwpm-batch-size"
					class="form__label"><?php esc_html_e( 'Items per request', 'coffeewp-media-cleanup' ); ?></label>
				<input type="number" id="cwpm-batch-size" name="cwpm_batch_size" class="form__input" min="1" max="500"
					value="<?php echo esc_attr( $batch_size ); ?>" />
				<span
					class="form__hint"><?php esc_html_e( 'Used for scan, backup, and clean. Lower values reduce timeouts; higher values finish faster if your server allows.', 'coffeewp-media-cleanup' ); ?></span>
			</div>
		</div>

		<?php
		/**
		 * Filter — controls whether the "Posts and Pages only" scope notice
		 * appears. Defaults to true. Extension plugins that extend the scanner
		 * to cover more post types may filter to false to suppress the notice.
		 *
		 * @since 1.3.0
		 *
		 * @param bool $show Whether to render the notice.
		 */
		if ( apply_filters( 'cwpm_show_posts_pages_only_notice', true ) ) :
			?>
			<div>
				<p>
					<?php
					echo wp_kses(
						__( 'Only <strong>Posts</strong> and <strong>Pages</strong> are checked for media usage. Images used exclusively in other post types may be reported as unused.', 'coffeewp-media-cleanup' ),
						array( 'strong' => array() )
					);
					?>
				</p>
			</div>
		<?php endif; ?>

		<?php
		/**
		 * Extension point — extension plugins may render a Pre-Scan Options
		 * section here. The core plugin emits no default content.
		 *
		 * @since 1.3.0
		 */
		do_action( 'cwpm_prescan_options' );
		?>

		<!-- Thumbnail Sizes -->
		<div class="cwpm-settings-section">
			<ul class="accordion cwpm-thumbnails-accordion">
				<li class="accordion__item">
					<details class="accordion__details ">
						<summary class="accordion__summary ">
							<span class="accordion__label">
								<?php esc_html_e( 'Thumbnail Sizes', 'coffeewp-media-cleanup' ); ?>
								<span class="badge badge-sm badge-neutral">
									<?php
									printf(
										/* translators: %1$d: enabled count, %2$d: total count */
										esc_html__( '%1$d / %2$d enabled', 'coffeewp-media-cleanup' ),
										(int) $enabled,
										(int) $total
									);
									?>
								</span>
							</span>
							<span class="accordion__icon" aria-hidden="true">+</span>
						</summary>
						<div class="accordion__panel accordion__panel--no-padding">
							<p class="cwpm-settings-section__desc">
								<?php esc_html_e( 'Toggle off sizes you don\'t need to prevent WordPress from generating them.', 'coffeewp-media-cleanup' ); ?>
							</p>
							<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';
													$source_label = ucfirst( $size['source'] );
													if ( ! empty( $size['source_name'] ) && 'WordPress' !== $size['source_name'] ) {
														$source_label = $size['source_name'];
													}
													?>
													<span class="badge badge-sm <?php echo esc_attr( $badge_class ); ?>">
														<?php echo esc_html( $source_label ); ?>
													</span>
												</td>
											</tr>
										<?php endforeach; ?>
									</tbody>
								</table>
							</div>
						</div>
					</details>
				</li>
			</ul>
		</div>

		<div class="cwpm-step__footer cwpm-step__footer--center">
			<button type="button" class="btn btn--primary cwpm-next-step" data-next="2">
				<?php esc_html_e( '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>
