<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use WP_PowerSuite\AdminColorCustomizer\Admin_Helpers;
use WP_PowerSuite\AdminColorCustomizer\FeatureGate;

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

$builder_scheme = is_array( $active_scheme ) ? $active_scheme : ( $built_in['powersuite_blue'] ?? [] );
$preset_defaults = $built_in[ (string) ( $builder_scheme['id'] ?? '' ) ] ?? $builder_scheme;
$color_groups   = [
	__( 'Brand', 'wp-powersuite' ) => [
		'primary'   => __( 'Primary', 'wp-powersuite' ),
		'secondary' => __( 'Secondary', 'wp-powersuite' ),
		'accent'    => __( 'Accent', 'wp-powersuite' ),
	],
	__( 'Admin menu', 'wp-powersuite' ) => [
		'menu_bg'         => __( 'Background', 'wp-powersuite' ),
		'menu_text'       => __( 'Text', 'wp-powersuite' ),
		'menu_hover_bg'   => __( 'Hover background', 'wp-powersuite' ),
		'menu_hover_text' => __( 'Hover text', 'wp-powersuite' ),
	],
	__( 'Toolbar', 'wp-powersuite' ) => [
		'toolbar_bg'   => __( 'Background', 'wp-powersuite' ),
		'toolbar_text' => __( 'Text', 'wp-powersuite' ),
	],
	__( 'Buttons & notices', 'wp-powersuite' ) => [
		'button_bg'   => __( 'Button background', 'wp-powersuite' ),
		'button_text' => __( 'Button text', 'wp-powersuite' ),
		'notice'      => __( 'Notice accent', 'wp-powersuite' ),
	],
];
?>
<div class="wpps-acc-panel">
	<h2><?php esc_html_e( 'Colors', 'wp-powersuite' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Pick a preset or fine-tune individual colors. Changes preview instantly — save when ready.', 'wp-powersuite' ); ?></p>

	<h3><?php esc_html_e( 'Presets', 'wp-powersuite' ); ?></h3>
	<div class="wpps-acc-preset-grid">
		<?php foreach ( $built_in as $scheme_id => $scheme ) : ?>
			<label class="wpps-acc-preset-card">
				<input
					type="radio"
					name="wpps_acc_active_scheme"
					value="<?php echo esc_attr( $scheme_id ); ?>"
					data-setting="active_scheme"
					<?php checked( (string) ( $settings['active_scheme'] ?? '' ), $scheme_id ); ?>
				/>
				<span class="wpps-acc-preset-card__swatches">
					<?php foreach ( [ 'menu_bg', 'primary', 'accent', 'button_bg' ] as $color_key ) : ?>
						<span style="background-color: <?php echo esc_attr( (string) ( $scheme[ $color_key ] ?? '#000' ) ); ?>"></span>
					<?php endforeach; ?>
				</span>
				<strong><?php echo esc_html( (string) ( $scheme['name'] ?? $scheme_id ) ); ?></strong>
			</label>
		<?php endforeach; ?>
	</div>

	<hr class="wpps-acc-divider" />

	<div class="wpps-acc-customize-section<?php echo $premium_unlocked ? '' : ' wpps-acc-customize-section--locked'; ?>">
		<h3>
			<?php esc_html_e( 'Customize active scheme', 'wp-powersuite' ); ?>
			<?php if ( ! $premium_unlocked ) : ?>
				<span class="wpps-pro-badge"><?php esc_html_e( 'Premium', 'wp-powersuite' ); ?></span>
			<?php endif; ?>
		</h3>
		<?php if ( ! $premium_unlocked ) : ?>
			<p class="description">
				<?php
				echo wp_kses_post(
					sprintf(
						/* translators: %s: URL to license page */
						__( 'Fine-tuning individual colors and saving custom schemes requires a license. <a href="%s">Activate your license</a> to unlock the color builder.', 'wp-powersuite' ),
						esc_url( $license_page_url )
					)
				);
				?>
			</p>
		<?php endif; ?>

		<input type="hidden" data-builder-field="id" value="<?php echo esc_attr( (string) ( $builder_scheme['id'] ?? 'custom_scheme' ) ); ?>" />

		<?php foreach ( $color_groups as $group_label => $fields ) : ?>
			<div class="wpps-acc-color-group">
				<h4><?php echo esc_html( $group_label ); ?></h4>
				<div class="wpps-acc-color-grid">
					<?php foreach ( $fields as $field_key => $field_label ) : ?>
						<?php
						Admin_Helpers::builder_color_field(
							$field_key,
							$field_label,
							(string) ( $builder_scheme[ $field_key ] ?? '#000000' ),
							(string) ( $preset_defaults[ $field_key ] ?? '#000000' ),
							! $premium_unlocked
						);
						?>
					<?php endforeach; ?>
				</div>
			</div>
		<?php endforeach; ?>

	</div>
</div>
