<?php
/**
 * Shared sticky action bar partial.
 *
 * Renders a configurable action bar for both the Scan Results and Trash views.
 * Each element's ID is prefixed by the id_prefix config so both instances can
 * coexist on the page without ID collisions.
 *
 * Config keys (all boolean unless noted):
 *
 * @param string  $id_prefix          Prefix for all element IDs.
 * @param bool    $search             Show search input.
 * @param bool    $status_filter      Show status dropdown (scan results only).
 * @param bool    $actions_button     Show "Actions" button.
 * @param bool    $primary_cta        Show primary CTA button.
 * @param string  $primary_cta_label  Label for primary CTA.
 * @param string  $primary_cta_id     ID for primary CTA button.
 * @param string  $primary_cta_class  Extra CSS class for primary CTA.
 * @param string  $primary_cta_icon   FontAwesome icon class.
 * @param bool    $compact_toggle     Show compact sizes switch.
 * @param bool    $group_toggle       Show group-by-month switch.
 * @param bool    $pagination         Show pagination controls.
 * @param bool    $select_visible     Show "Select Visible" checkbox.
 * @param bool    $select_all         Show "Select All" checkbox.
 * @param bool    $clean_progress     Show clean progress overlay.
 *
 * @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.

$cwpm_bar_config = wp_parse_args(
	$cwpm_bar_config,
	array(
		'id_prefix'           => 'cwpm',
		'search'              => false,
		'status_filter'       => false,
		'actions_button'      => false,
		'primary_cta'         => false,
		'primary_cta_label'   => __( 'Run Actions', 'coffeewp-media-cleanup' ),
		'primary_cta_id'      => 'cwpm-bar-clean',
		'primary_cta_class'   => 'btn--primary',
		'primary_cta_icon'    => 'fa-play',
		'primary_cta_title'   => __( 'Run selected actions', 'coffeewp-media-cleanup' ),
		'secondary_cta'       => false,
		'secondary_cta_label' => '',
		'secondary_cta_id'    => '',
		'secondary_cta_class' => 'btn--danger',
		'secondary_cta_icon'  => '',
		'secondary_cta_title' => '',
		'compact_toggle'      => false,
		'group_toggle'        => false,
		'pagination'          => false,
		'select_visible'      => false,
		'select_all'          => false,
		'clean_progress'      => false,
	)
);

$p = $cwpm_bar_config['id_prefix'];
?>
<div class="cwpm-action-bar" id="<?php echo esc_attr( $p ); ?>-action-bar" style="display: none;">

	<?php if ( $cwpm_bar_config['clean_progress'] ) : ?>
		<!-- Clean progress (replaces normal controls during cleaning) -->
		<div class="cwpm-action-bar__clean" id="<?php echo esc_attr( $p ); ?>-bar-clean-progress" style="display: none;">
			<div class="cwpm-action-bar__clean-info">
				<span class="cwpm-action-bar__clean-status"
					id="<?php echo esc_attr( $p ); ?>-bar-clean-status"><?php esc_html_e( 'Cleaning...', 'coffeewp-media-cleanup' ); ?></span>
				<span class="cwpm-action-bar__clean-file" id="<?php echo esc_attr( $p ); ?>-bar-clean-file"></span>
			</div>
			<div class="cwpm-progress cwpm-action-bar__clean-bar">
				<div class="cwpm-progress__bar" id="<?php echo esc_attr( $p ); ?>-bar-clean-progress-bar" style="width: 0%;">
				</div>
			</div>
		</div>
	<?php endif; ?>

	<!-- Row 1: Search, Status, Actions, Primary CTA -->
	<div class="cwpm-action-bar__row" id="<?php echo esc_attr( $p ); ?>-action-bar-row1">

		<?php if ( $cwpm_bar_config['search'] ) : ?>
			<!-- Search -->
			<div class="cwpm-action-bar__search">
				<input type="text" id="<?php echo esc_attr( $p ); ?>-search-filter"
					class="form__input cwpm-action-bar__search-input"
					placeholder="<?php esc_attr_e( 'Search files...', 'coffeewp-media-cleanup' ); ?>" />
				<span class="cwpm-action-bar__search-icon" aria-hidden="true"><i
						class="fa-solid fa-magnifying-glass"></i></span>
			</div>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['status_filter'] ) : ?>
			<!-- Status filter (Color Punch dropdown) -->
			<div class="dropdown cwpm-status-dropdown" data-dropdown>
				<input type="hidden" id="<?php echo esc_attr( $p ); ?>-status-filter"
					name="<?php echo esc_attr( $p ); ?>_status_filter" value="" />
				<button type="button"
					class="dropdown__trigger"><?php esc_html_e( 'All Status', 'coffeewp-media-cleanup' ); ?></button>
				<ul class="dropdown__panel" hidden>
					<li class="dropdown__option" data-value="" tabindex="0">
						<?php esc_html_e( 'All Status', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="missing" tabindex="0">
						<?php esc_html_e( 'Not in Media Library', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="unused" tabindex="0">
						<?php esc_html_e( 'Unused', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="used" tabindex="0">
						<?php esc_html_e( 'Used', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="broken" tabindex="0">
						<?php esc_html_e( 'Broken', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="missing-thumbs" tabindex="0">
						<?php esc_html_e( 'Missing Thumbnails', 'coffeewp-media-cleanup' ); ?>
					</li>
					<li class="dropdown__option" data-value="orphan-thumbs" tabindex="0">
						<?php esc_html_e( 'Orphan Thumbnails', 'coffeewp-media-cleanup' ); ?>
					</li>
									</ul>
			</div>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['actions_button'] ) : ?>
			<!-- Actions (opens modal) -->
			<input type="hidden" name="cwpm_selected_actions" id="<?php echo esc_attr( $p ); ?>-selected-actions" value="" />
			<button type="button" class="btn btn--outline btn--no-shadow btn--rounded cwpm-actions-trigger"
				id="<?php echo esc_attr( $p ); ?>-actions-trigger">
				<i class="fa-solid fa-list-check" aria-hidden="true"></i>
				<?php esc_html_e( 'Actions', 'coffeewp-media-cleanup' ); ?>
				<span class="cwpm-actions-count" id="<?php echo esc_attr( $p ); ?>-actions-count" hidden></span>
			</button>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['secondary_cta'] ) : ?>
			<!-- Secondary CTA -->
			<button type="button"
				class="btn <?php echo esc_attr( $cwpm_bar_config['secondary_cta_class'] ); ?> btn--no-shadow btn--rounded"
				id="<?php echo esc_attr( $cwpm_bar_config['secondary_cta_id'] ); ?>"
				title="<?php echo esc_attr( $cwpm_bar_config['secondary_cta_title'] ); ?>">
				<i class="fa-solid <?php echo esc_attr( $cwpm_bar_config['secondary_cta_icon'] ); ?>" aria-hidden="true"></i>
				<?php echo esc_html( $cwpm_bar_config['secondary_cta_label'] ); ?>
			</button>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['primary_cta'] ) : ?>
			<!-- Primary CTA -->
			<button type="button"
				class="btn <?php echo esc_attr( $cwpm_bar_config['primary_cta_class'] ); ?> btn--no-shadow btn--rounded cwpm-action-bar__play"
				id="<?php echo esc_attr( $cwpm_bar_config['primary_cta_id'] ); ?>"
				title="<?php echo esc_attr( $cwpm_bar_config['primary_cta_title'] ); ?>">
				<i class="fa-solid <?php echo esc_attr( $cwpm_bar_config['primary_cta_icon'] ); ?>" aria-hidden="true"></i>
				<?php echo esc_html( $cwpm_bar_config['primary_cta_label'] ); ?>
			</button>
		<?php endif; ?>

	</div>

	<!-- Row 2: Toggles, Pagination, Select controls -->
	<div class="cwpm-action-bar__row cwpm-action-bar__row--secondary" id="<?php echo esc_attr( $p ); ?>-action-bar-row2">

		<?php if ( $cwpm_bar_config['compact_toggle'] ) : ?>
			<!-- Compact sizes switch -->
			<label class="switch cwpm-action-bar__switch"
				title="<?php esc_attr_e( 'Compact thumbnail sizes', 'coffeewp-media-cleanup' ); ?>">
				<input type="checkbox" class="switch__input" id="<?php echo esc_attr( $p ); ?>-compact-sizes" checked />
				<span class="switch__track" aria-hidden="true"></span>
			</label>
			<span
				class="cwpm-action-bar__switch-label"><?php esc_html_e( 'Compact Thumbnail View', 'coffeewp-media-cleanup' ); ?></span>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['group_toggle'] ) : ?>
			<!-- Group by Month switch -->
			<label class="switch cwpm-action-bar__switch"
				title="<?php esc_attr_e( 'Group results by year/month folders', 'coffeewp-media-cleanup' ); ?>">
				<input type="checkbox" class="switch__input" id="<?php echo esc_attr( $p ); ?>-group-by-month" checked />
				<span class="switch__track" aria-hidden="true"></span>
			</label>
			<span
				class="cwpm-action-bar__switch-label"><?php esc_html_e( 'Group by Month', 'coffeewp-media-cleanup' ); ?></span>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['pagination'] ) : ?>
			<!-- Pagination -->
			<div class="cwpm-pagination" id="<?php echo esc_attr( $p ); ?>-pagination">
				<span class="cwpm-pagination__label"><?php esc_html_e( 'Per page', 'coffeewp-media-cleanup' ); ?></span>
				<div class="dropdown cwpm-pagination__dropdown" data-dropdown>
					<input type="hidden" id="<?php echo esc_attr( $p ); ?>-per-page"
						name="<?php echo esc_attr( $p ); ?>_per_page" value="200" />
					<button type="button" class="dropdown__trigger">200</button>
					<ul class="dropdown__panel" hidden>
						<li class="dropdown__option" data-value="25" tabindex="0">25</li>
						<li class="dropdown__option" data-value="50" tabindex="0">50</li>
						<li class="dropdown__option" data-value="100" tabindex="0">100</li>
						<li class="dropdown__option" data-value="200" tabindex="0" aria-selected="true">200</li>
						<li class="dropdown__option" data-value="500" tabindex="0">500</li>
						<li class="dropdown__option" data-value="1000" tabindex="0">1000</li>
						<li class="dropdown__option" data-value="0" tabindex="0">
							<?php esc_html_e( 'All', 'coffeewp-media-cleanup' ); ?>
						</li>
					</ul>
				</div>
				<span class="cwpm-pagination__info" id="<?php echo esc_attr( $p ); ?>-pagination-info"></span>
				<button type="button" class="btn btn--outline btn--no-shadow no-lift btn--rounded cwpm-pagination__btn"
					id="<?php echo esc_attr( $p ); ?>-page-prev" disabled>
					<i class="fa-solid fa-chevron-left" aria-hidden="true"></i>
				</button>
				<button type="button" class="btn btn--outline btn--no-shadow no-lift btn--rounded cwpm-pagination__btn"
					id="<?php echo esc_attr( $p ); ?>-page-next" disabled>
					<i class="fa-solid fa-chevron-right" aria-hidden="true"></i>
				</button>
			</div>
		<?php endif; ?>

		<?php if ( $cwpm_bar_config['select_visible'] || $cwpm_bar_config['select_all'] ) : ?>
		<div class="cwpm-action-bar__selects">

			<?php if ( $cwpm_bar_config['select_visible'] ) : ?>
			<div class="cwpm-action-bar__select-pair">
				<span
					class="cwpm-action-bar__select-label"><?php esc_html_e( 'Select Visible', 'coffeewp-media-cleanup' ); ?></span>
				<label class="form__check cwpm-action-bar__select-check"
					title="<?php esc_attr_e( 'Select visible files on this page', 'coffeewp-media-cleanup' ); ?>">
					<input type="checkbox" id="<?php echo esc_attr( $p ); ?>-select-visible"
						aria-label="<?php esc_attr_e( 'Select visible files', 'coffeewp-media-cleanup' ); ?>" />
					<span aria-hidden="true"></span>
				</label>
			</div>
			<?php endif; ?>

			<?php if ( $cwpm_bar_config['select_all'] ) : ?>
			<div class="cwpm-action-bar__select-pair">
				<span class="cwpm-action-bar__select-label"><?php esc_html_e( 'Select All', 'coffeewp-media-cleanup' ); ?></span>
				<label class="form__check cwpm-action-bar__select-check"
					title="<?php esc_attr_e( 'Select all files across all pages', 'coffeewp-media-cleanup' ); ?>">
					<input type="checkbox" id="<?php echo esc_attr( $p ); ?>-select-all"
						aria-label="<?php esc_attr_e( 'Select all files', 'coffeewp-media-cleanup' ); ?>" />
					<span aria-hidden="true"></span>
				</label>
			</div>
			<?php endif; ?>

		</div>
		<?php endif; ?>

	</div>
</div>
