<?php

namespace HideAdminBar;

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

if ( ! isset( $module_id ) ) {
	$module_id = 'hide-admin-bar';
}

if ( ! class_exists( Module::class ) ) {
	$module_file = __DIR__ . '/module.php';
	if ( is_readable( $module_file ) ) {
		require_once $module_file;
	}
}

$settings_repo = OptionStoreModuleSettingsRepository::forModule( $module_id );
$raw_settings  = $settings_repo->all( [] );
if ( ! is_array( $raw_settings ) ) {
	$raw_settings = [];
}

$settings = class_exists( Module::class )
	? Module::normalizeSettings( $raw_settings )
	: array_merge( [ 'roles' => [ 'subscriber' ], 'always_show_for_admins' => false ], $raw_settings );

$needs_migration = false;
if ( isset( $raw_settings['roles'] ) && is_array( $raw_settings['roles'] ) && $raw_settings['roles'] !== [] && ! array_is_list( $raw_settings['roles'] ) ) {
	$needs_migration = true;
} else {
	foreach ( array_keys( $raw_settings ) as $legacy_key ) {
		if ( $legacy_key !== 'roles' && strpos( $legacy_key, 'roles' ) === 0 && strlen( $legacy_key ) > 5 ) {
			$needs_migration = true;
			break;
		}
	}
}

if ( $needs_migration ) {
	$settings_repo->save( $settings );
}

$selected_roles           = $settings['roles'];
$always_show_for_admins   = ! empty( $settings['always_show_for_admins'] );
$all_roles                = wp_roles()->get_names();

?>
<div class="wp-powersuite-module-settings">
	<p class="description">
		<?php esc_html_e( 'Select user roles for which the admin bar should be hidden on the frontend (public-facing pages). The admin bar always remains visible in the WordPress dashboard.', 'wp-powersuite' ); ?>
	</p>

	<div class="notice notice-warning inline" style="margin: 12px 0;">
		<p>
			<?php esc_html_e( 'These settings override the “Show Toolbar when viewing site” option on user profile screens.', 'wp-powersuite' ); ?>
		</p>
	</div>

	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Hide for Roles:', 'wp-powersuite' ); ?></th>
					<td>
						<?php if ( empty( $all_roles ) ) : ?>
							<p class="description"><?php esc_html_e( 'No roles found.', 'wp-powersuite' ); ?></p>
						<?php else : ?>
							<p class="description" style="margin-bottom: 8px;"><?php esc_html_e( 'Select roles that should not see the admin bar on the frontend.', 'wp-powersuite' ); ?></p>
							<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_roles_ms"
								data-label-none="<?php echo esc_attr( __( 'Select roles...', 'wp-powersuite' ) ); ?>"
								data-label-single="<?php echo esc_attr( __( '1 selected', 'wp-powersuite' ) ); ?>"
								<?php /* translators: %d: number value. */ ?>
								data-label-multi="<?php echo esc_attr( __( '%d selected', 'wp-powersuite' ) ); ?>">
								<button type="button" class="wpps-ct-multiselect__trigger" aria-expanded="false" aria-haspopup="listbox" aria-controls="<?php echo esc_attr( $module_id ); ?>_roles_list">
									<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select roles...', 'wp-powersuite' ); ?></span>
									<span class="dashicons dashicons-arrow-down-alt2 wpps-ct-multiselect__chevron"></span>
								</button>
								<div class="wpps-ct-multiselect__dropdown" id="<?php echo esc_attr( $module_id ); ?>_roles_list" role="listbox" hidden>
									<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search roles...', 'wp-powersuite' ); ?>" autocomplete="off" />
									<div class="wpps-ct-multiselect__actions">
										<button type="button" class="wpps-ct-multiselect__action" data-action="select-all"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></button>
										<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wp-powersuite' ); ?></button>
									</div>
									<div class="wpps-ct-multiselect__options">
										<?php foreach ( $all_roles as $role_key => $role_label ) : ?>
											<?php $checked = in_array( $role_key, $selected_roles, true ); ?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $role_key ); ?>" data-label="<?php echo esc_attr( $role_label ); ?>">
												<input type="checkbox" name="roles[]" class="wp-powersuite-advanced-setting" data-setting-key="roles" value="<?php echo esc_attr( $role_key ); ?>" <?php checked( $checked ); ?> />
												<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $role_label ); ?></span>
												<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $role_key ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Administrators', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input
								type="checkbox"
								name="always_show_for_admins"
								value="1"
								class="wp-powersuite-advanced-setting"
								data-setting-key="always_show_for_admins"
								<?php checked( $always_show_for_admins ); ?>
							/>
							<?php esc_html_e( 'Always show the admin bar for administrators on the frontend', 'wp-powersuite' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'Useful when an administrator has multiple roles and the admin bar is hidden for another role.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>

	<?php if ( ! empty( $all_roles ) ) : ?>
	<style>
		.wpps-ct-multiselect { position: relative; max-width: 360px; }
		.wpps-ct-multiselect__trigger { display: flex; align-items: center; justify-content: space-between; width: 100%; padding: 8px 12px; border: 1px solid #8c8f94; border-radius: 4px; background: #fff; font-size: 14px; text-align: left; cursor: pointer; line-height: 1.4; color: #1d2327; }
		.wpps-ct-multiselect__trigger:hover { border-color: #2271b1; }
		.wpps-ct-multiselect__chevron { font-size: 18px; width: 18px; height: 18px; color: #787c82; flex-shrink: 0; margin-left: 8px; }
		.wpps-ct-multiselect__trigger[aria-expanded="true"] .wpps-ct-multiselect__chevron { transform: rotate(180deg); }
		.wpps-ct-multiselect__dropdown { position: absolute; top: 100%; left: 0; right: 0; margin-top: 4px; border: 1px solid #8c8f94; border-radius: 4px; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.12); z-index: 100; max-height: 280px; display: flex; flex-direction: column; }
		.wpps-ct-multiselect__dropdown[hidden] { display: none !important; }
		.wpps-ct-multiselect__search { width: 100%; padding: 8px 12px; border: none; border-bottom: 1px solid #dcdcde; font-size: 13px; box-sizing: border-box; outline: none; }
		.wpps-ct-multiselect__search::placeholder { color: #a7aaad; }
		.wpps-ct-multiselect__actions { padding: 6px 10px; border-bottom: 1px solid #dcdcde; display: flex; gap: 12px; flex-shrink: 0; }
		.wpps-ct-multiselect__action { background: none; border: none; color: #2271b1; cursor: pointer; font-size: 12px; padding: 0; }
		.wpps-ct-multiselect__action:hover { color: #135e96; text-decoration: underline; }
		.wpps-ct-multiselect__options { overflow-y: auto; padding: 4px 0; max-height: 200px; }
		.wpps-ct-multiselect__option { display: flex; align-items: center; gap: 6px; padding: 6px 12px; cursor: pointer; font-size: 13px; white-space: nowrap; }
		.wpps-ct-multiselect__option:hover { background: #f0f0f1; }
		.wpps-ct-multiselect__option input { flex-shrink: 0; margin: 0; }
		.wpps-ct-multiselect__option-slug { color: #646970; font-size: 12px; }
		.wpps-ct-multiselect__option.wpps-ct-multiselect__option--hidden { display: none; }
	</style>
	<?php endif; ?>
</div>

