<?php
/**
 * Setup tab — how colors are applied.
 */

use WP_PowerSuite\AdminColorCustomizer\FeatureGate;
use WP_PowerSuite\AdminColorCustomizer\Settings;

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

$premium_unlocked = $premium_unlocked ?? FeatureGate::isPremiumLicensed();
$license_page_url = $license_page_url ?? admin_url( 'admin.php?page=powersuite-license' );

$mode_help = [
	'disabled'            => __( 'No custom admin colors are output.', 'wp-powersuite' ),
	'global_default'      => __( 'Apply the active scheme to everyone. Users can still pick a PowerSuite scheme in their profile.', 'wp-powersuite' ),
	'force_all'           => __( 'Always use the active scheme for every user.', 'wp-powersuite' ),
	'force_by_role'       => __( 'Map WordPress roles to schemes on the Advanced tab.', 'wp-powersuite' ),
	'per_user_selectable' => __( 'Register custom schemes in the profile color picker. Falls back to the active scheme.', 'wp-powersuite' ),
];
?>
<div class="wpps-acc-panel">
	<h2><?php esc_html_e( 'Setup', 'wp-powersuite' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Choose how this module applies colors across wp-admin. Pick a preset on the Colors tab, then save.', 'wp-powersuite' ); ?></p>

	<table class="form-table" role="presentation">
		<tbody>
			<tr>
				<th scope="row"><label for="wpps-acc-mode"><?php esc_html_e( 'Application mode', 'wp-powersuite' ); ?></label></th>
				<td>
					<select id="wpps-acc-mode" data-setting="mode">
						<?php foreach ( Settings::MODES as $mode ) : ?>
							<?php
							$is_premium_mode = in_array( $mode, FeatureGate::PREMIUM_MODES, true );
							?>
							<option
								value="<?php echo esc_attr( $mode ); ?>"
								<?php selected( (string) ( $settings['mode'] ?? 'global_default' ), $mode ); ?>
								<?php disabled( ! $premium_unlocked && $is_premium_mode ); ?>
							><?php echo esc_html( $mode_labels[ $mode ] ?? $mode ); ?><?php echo ( ! $premium_unlocked && $is_premium_mode ) ? ' (' . esc_html__( 'Premium', 'wp-powersuite' ) . ')' : ''; ?></option>
						<?php endforeach; ?>
					</select>
					<?php if ( ! $premium_unlocked ) : ?>
						<p class="description">
							<?php
							echo wp_kses_post(
								sprintf(
									/* translators: %s: URL to license page */
									__( 'Force for all users and Force by role require a license. <a href="%s">Activate your license</a> to unlock them.', 'wp-powersuite' ),
									esc_url( $license_page_url )
								)
							);
							?>
						</p>
					<?php endif; ?>
					<p class="description" id="wpps-acc-mode-help"><?php echo esc_html( $mode_help[ (string) ( $settings['mode'] ?? 'global_default' ) ] ?? '' ); ?></p>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="wpps-acc-scope"><?php esc_html_e( 'Where to apply', 'wp-powersuite' ); ?></label></th>
				<td>
					<select id="wpps-acc-scope" data-setting="scope">
						<option value="admin_only" <?php selected( (string) ( $settings['scope'] ?? '' ), 'admin_only' ); ?>><?php esc_html_e( 'WordPress admin dashboard only', 'wp-powersuite' ); ?></option>
						<option value="admin_and_admin_bar" <?php selected( (string) ( $settings['scope'] ?? '' ), 'admin_and_admin_bar' ); ?>><?php esc_html_e( 'Admin dashboard + frontend admin bar (logged-in users)', 'wp-powersuite' ); ?></option>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><?php esc_html_e( 'Active scheme', 'wp-powersuite' ); ?></th>
				<td>
					<select data-setting="active_scheme" id="wpps-acc-active-scheme-select">
						<?php foreach ( $schemes as $scheme_id => $scheme ) : ?>
							<option value="<?php echo esc_attr( $scheme_id ); ?>" <?php selected( (string) ( $settings['active_scheme'] ?? '' ), $scheme_id ); ?>>
								<?php echo esc_html( (string) ( $scheme['name'] ?? $scheme_id ) ); ?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Used as the site default and fallback. Customize colors on the Colors tab.', 'wp-powersuite' ); ?></p>
				</td>
			</tr>
		</tbody>
	</table>

	<?php if ( ! empty( $accessibility ) ) : ?>
		<div class="notice notice-warning inline">
			<p><strong><?php esc_html_e( 'Accessibility warnings for the active scheme', 'wp-powersuite' ); ?></strong></p>
			<ul>
				<?php foreach ( $accessibility as $warning ) : ?>
					<li>
						<?php
						printf(
							esc_html__( '%1$s — contrast ratio %2$s (minimum 4.5)', 'wp-powersuite' ),
							esc_html( (string) ( $warning['label'] ?? '' ) ),
							esc_html( (string) ( $warning['ratio'] ?? '' ) )
						);
						?>
					</li>
				<?php endforeach; ?>
			</ul>
		</div>
	<?php endif; ?>
</div>
