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

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

$premium_unlocked = $premium_unlocked ?? FeatureGate::isPremiumLicensed();

$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', 'wppowersuite-lite' ) => [
		'primary'   => __( 'Primary', 'wppowersuite-lite' ),
		'secondary' => __( 'Secondary', 'wppowersuite-lite' ),
		'accent'    => __( 'Accent', 'wppowersuite-lite' ),
	],
	__( 'Admin menu', 'wppowersuite-lite' ) => [
		'menu_bg'         => __( 'Background', 'wppowersuite-lite' ),
		'menu_text'       => __( 'Text', 'wppowersuite-lite' ),
		'menu_hover_bg'   => __( 'Hover background', 'wppowersuite-lite' ),
		'menu_hover_text' => __( 'Hover text', 'wppowersuite-lite' ),
	],
	__( 'Toolbar', 'wppowersuite-lite' ) => [
		'toolbar_bg'   => __( 'Background', 'wppowersuite-lite' ),
		'toolbar_text' => __( 'Text', 'wppowersuite-lite' ),
	],
	__( 'Buttons & notices', 'wppowersuite-lite' ) => [
		'button_bg'   => __( 'Button background', 'wppowersuite-lite' ),
		'button_text' => __( 'Button text', 'wppowersuite-lite' ),
		'notice'      => __( 'Notice accent', 'wppowersuite-lite' ),
	],
];
?>
<div class="wpps-acc-panel">
	<h2><?php esc_html_e( 'Colors', 'wppowersuite-lite' ); ?></h2>
	<p class="description"><?php esc_html_e( 'Pick a preset or fine-tune individual colors. Changes preview instantly — save when ready.', 'wppowersuite-lite' ); ?></p>

	<h3><?php esc_html_e( 'Presets', 'wppowersuite-lite' ); ?></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', 'wppowersuite-lite' ); ?>
			<?php if ( ! $premium_unlocked && function_exists( 'wpps_render_premium_badge' ) ) : ?>
				<?php wpps_render_premium_badge(); ?>
			<?php endif; ?>
		</h3>
		<?php if ( ! $premium_unlocked && function_exists( 'wpps_render_premium_upsell_notice' ) ) : ?>
			<?php wpps_render_premium_upsell_notice(); ?>
		<?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>
