<?php
use WP_PowerSuite\AdminColorCustomizer\Admin_Helpers;
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' );

$role_schemes = is_array( $settings['role_schemes'] ?? null ) ? $settings['role_schemes'] : [];
$env          = is_array( $settings['environment_badge'] ?? null ) ? $settings['environment_badge'] : [];
?>
<div class="wpps-acc-panel">
	<h2><?php esc_html_e( 'Advanced', 'wp-powersuite' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Optional features: role-based schemes, environment badge, and custom CSS.', 'wp-powersuite' ); ?></p>

	<div class="wpps-acc-advanced-section<?php echo $premium_unlocked ? '' : ' wpps-acc-advanced-section--locked'; ?>">
		<h3>
			<?php esc_html_e( 'Role mapping', 'wp-powersuite' ); ?>
			<?php if ( ! $premium_unlocked ) : ?>
				<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span>
			<?php endif; ?>
		</h3>
		<p class="description"><?php esc_html_e( 'Only used when mode is “Force by role”. Administrator wins over editor, author, contributor, subscriber, then custom roles alphabetically.', 'wp-powersuite' ); ?></p>
		<?php if ( ! $premium_unlocked ) : ?>
			<p class="description">
				<?php
				echo wp_kses_post(
					sprintf(
						/* translators: %s: URL to license page */
						__( 'Role mapping requires a license. <a href="%s">Activate your license</a> to assign schemes by role.', 'wp-powersuite' ),
						esc_url( $license_page_url )
					)
				);
				?>
			</p>
		<?php endif; ?>
		<table class="widefat striped wpps-acc-role-table">
			<thead>
				<tr>
					<th scope="col"><?php esc_html_e( 'Role', 'wp-powersuite' ); ?></th>
					<th scope="col"><?php esc_html_e( 'Scheme', 'wp-powersuite' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $editable_roles as $role_key => $role_data ) : ?>
					<tr>
						<td><?php echo esc_html( translate_user_role( (string) ( $role_data['name'] ?? $role_key ) ) ); ?></td>
						<td>
							<select data-role-scheme="<?php echo esc_attr( (string) $role_key ); ?>" <?php disabled( ! $premium_unlocked ); ?>>
								<option value=""><?php esc_html_e( '— Use active scheme —', 'wp-powersuite' ); ?></option>
								<?php foreach ( $schemes as $scheme_id => $scheme ) : ?>
									<option value="<?php echo esc_attr( $scheme_id ); ?>" <?php selected( (string) ( $role_schemes[ $role_key ] ?? '' ), $scheme_id ); ?>>
										<?php echo esc_html( (string) ( $scheme['name'] ?? $scheme_id ) ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

	<div class="wpps-acc-advanced-section">
		<h3><?php esc_html_e( 'Environment badge', 'wp-powersuite' ); ?></h3>
		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row"><?php esc_html_e( 'Enable', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox" data-setting="environment_badge.enabled" <?php checked( ! empty( $env['enabled'] ) ); ?> />
							<?php esc_html_e( 'Show a label in the admin bar', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="wpps-acc-env-type"><?php esc_html_e( 'Type', 'wp-powersuite' ); ?></label></th>
					<td>
						<select id="wpps-acc-env-type" data-setting="environment_badge.type">
							<?php foreach ( [ 'staging', 'development', 'production', 'custom' ] as $type ) : ?>
								<option value="<?php echo esc_attr( $type ); ?>" <?php selected( (string) ( $env['type'] ?? 'production' ), $type ); ?>>
									<?php echo esc_html( ucfirst( $type ) ); ?>
								</option>
							<?php endforeach; ?>
						</select>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="wpps-acc-env-label"><?php esc_html_e( 'Custom label', 'wp-powersuite' ); ?></label></th>
					<td>
						<input type="text" id="wpps-acc-env-label" class="regular-text" data-setting="environment_badge.label" value="<?php echo esc_attr( (string) ( $env['label'] ?? '' ) ); ?>" />
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="wpps-acc-env-color"><?php esc_html_e( 'Badge color', 'wp-powersuite' ); ?></label></th>
					<td>
						<?php
						Admin_Helpers::setting_color_field(
							'environment_badge.color',
							__( 'Badge color', 'wp-powersuite' ),
							(string) ( $env['color'] ?? '#cc0000' ),
							'#cc0000',
							'wpps-acc-env-color'
						);
						?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Tint admin bar', 'wp-powersuite' ); ?></th>
					<td>
						<label>
							<input type="checkbox" data-setting="environment_badge.apply_bar_color" <?php checked( ! empty( $env['apply_bar_color'] ) ); ?> />
							<?php esc_html_e( 'Apply badge color to the admin bar background', 'wp-powersuite' ); ?>
						</label>
					</td>
				</tr>
			</tbody>
		</table>
	</div>

	<div class="wpps-acc-advanced-section wpps-acc-custom-css-section">
		<h3><?php esc_html_e( 'Custom CSS', 'wp-powersuite' ); ?></h3>
		<label>
			<input type="checkbox" id="wpps-acc-allow-custom-css" data-setting="allow_custom_css" <?php checked( ! empty( $settings['allow_custom_css'] ) ); ?> />
			<?php esc_html_e( 'Enable additional admin-only CSS (disabled by default)', 'wp-powersuite' ); ?>
		</label>
		<div class="wpps-acc-custom-css-editor-wrap">
			<div class="wpps-cs-edit-panel wpps-cs-code-panel wpps-hfc-code-panel wpps-acc-custom-css-panel" data-editor-height="220" style="--cs-editor-height: 220px;">
				<div class="wpps-cs-code-wrap">
					<textarea
						id="wpps-acc-custom-css"
						class="large-text code wpps-cs-code-editor"
						data-setting="custom_css"
						rows="10"
						placeholder="<?php esc_attr_e( '/* Additional admin-only CSS */', 'wp-powersuite' ); ?>"
						<?php disabled( empty( $settings['allow_custom_css'] ) ); ?>
					><?php echo esc_textarea( (string) ( $settings['custom_css'] ?? '' ) ); ?></textarea>
				</div>
			</div>
		</div>
	</div>

	<?php if ( is_multisite() ) : ?>
		<div class="wpps-acc-advanced-section">
			<h3><?php esc_html_e( 'Multisite', 'wp-powersuite' ); ?></h3>
			<?php if ( Settings::can_manage_network_settings() ) : ?>
				<label>
					<input type="checkbox" data-setting="network_default_enabled" <?php checked( ! empty( $network_settings['network_default_enabled'] ) ); ?> />
					<?php esc_html_e( 'Provide network default color settings', 'wp-powersuite' ); ?>
				</label><br />
				<label>
					<input type="checkbox" data-setting="network_force_enabled" <?php checked( ! empty( $network_settings['network_force_enabled'] ) ); ?> />
					<?php esc_html_e( 'Force network colors on all sites', 'wp-powersuite' ); ?>
				</label>
			<?php else : ?>
				<label>
					<input type="checkbox" data-setting="use_network_default" <?php checked( ! empty( $site_settings['use_network_default'] ) ); ?> />
					<?php esc_html_e( 'Use network default settings when available', 'wp-powersuite' ); ?>
				</label>
			<?php endif; ?>
		</div>
	<?php endif; ?>

	<p>
		<button type="button" class="button button-secondary" id="wpps-acc-reset-all"><?php esc_html_e( 'Reset all module settings', 'wp-powersuite' ); ?></button>
	</p>
</div>
