<?php
/**
 * Ready-made login page layout presets.
 *
 * @package LoginPageCustomizor
 */

namespace LoginPageCustomizor;

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

require_once __DIR__ . '/admin-helpers.php';

final class LayoutPresets {

	/**
	 * Unsplash image URL for preset backgrounds and thumbnails.
	 */
	private static function imageUrl( string $photo_id, int $width = 1920 ): string {
		return 'https://images.unsplash.com/' . $photo_id . '?auto=format&fit=crop&w=' . $width . '&q=80';
	}

	/**
	 * Mobile-friendly spacing applied to every preset unless a preset overrides these keys.
	 *
	 * @return array<string, mixed>
	 */
	private static function responsivePresetTweaks(): array {
		return [
			'box_padding_top_mobile'              => '20',
			'box_padding_right_mobile'            => '16',
			'box_padding_bottom_mobile'           => '20',
			'box_padding_left_mobile'             => '16',
			'box_border_radius_mobile'            => '10',
			'field_normal_padding_top_mobile'     => '10',
			'field_normal_padding_right_mobile'   => '12',
			'field_normal_padding_bottom_mobile'  => '10',
			'field_normal_padding_left_mobile'    => '12',
			'field_normal_border_radius_mobile'   => '6',
			'button_normal_padding_top_mobile'    => '10',
			'button_normal_padding_right_mobile'  => '16',
			'button_normal_padding_bottom_mobile' => '10',
			'button_normal_padding_left_mobile'   => '16',
			'button_normal_border_radius_mobile'  => '6',
		];
	}

	/** @return array<string, mixed> */
	private static function splitContainerStyles(): array {
		return [
			'box_bg_type'                          => 'color',
			'box_bg_color'                         => '#ffffff',
			'box_padding_top'                      => '32',
			'box_padding_right'                    => '40',
			'box_padding_bottom'                   => '32',
			'box_padding_left'                     => '40',
			'box_border_radius_desktop'            => '12',
			'box_border_radius_tablet'             => '12',
			'box_border_radius_mobile'             => '10',
			'box_shadow_x'                         => 0,
			'box_shadow_y'                         => 8,
			'box_shadow_blur'                      => 24,
			'box_shadow_spread'                    => 0,
			'box_shadow_color'                     => '#0000001a',
			'form_padding_top'                     => '10',
			'form_padding_right'                   => '10',
			'form_padding_bottom'                  => '10',
			'form_padding_left'                    => '10',
			'field_normal_padding_top'             => '12',
			'field_normal_padding_right'           => '14',
			'field_normal_padding_bottom'          => '12',
			'field_normal_padding_left'            => '14',
			'field_normal_border_type'             => 'solid',
			'field_normal_border_color'            => '#c3c4c7',
			'field_normal_border_width'            => '1',
			'field_normal_border_radius_desktop'   => '6',
			'field_normal_bg_color'                => '#ffffff',
			'button_normal_border_type'            => 'none',
			'button_hover_border_type'             => 'none',
			'button_hover_bg_type'                 => 'color',
		];
	}

	/** @return list<array{id: string, group: string, name: string, description: string}> */
	public static function getCatalog(): array {
		return [
			[
				'id'          => 'default',
				'group'       => 'default',
				'name'        => __( 'PowerSuite Default', 'wppowersuite-lite' ),
				'description' => __( 'Soft gradient canvas, elevated white card, and balanced form styling.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-clean',
				'group'       => 'classic',
				'name'        => __( 'Pure Minimal', 'wppowersuite-lite' ),
				'description' => __( 'Quiet gray backdrop, crisp card edges, and understated fields.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-night',
				'group'       => 'classic',
				'name'        => __( 'Midnight Focus', 'wppowersuite-lite' ),
				'description' => __( 'Deep navy scene with a bright card and electric cyan actions.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-sunrise',
				'group'       => 'classic',
				'name'        => __( 'Warm Sunrise', 'wppowersuite-lite' ),
				'description' => __( 'Peach gradient warmth with rounded fields and a bold orange button.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-forest',
				'group'       => 'classic',
				'name'        => __( 'Forest Calm', 'wppowersuite-lite' ),
				'description' => __( 'Full-bleed woodland photo with a clean card and earthy green sign-in.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-glass',
				'group'       => 'classic',
				'name'        => __( 'City Glass', 'wppowersuite-lite' ),
				'description' => __( 'Night skyline photo behind a soft white card and indigo accents.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'classic-ocean',
				'group'       => 'classic',
				'name'        => __( 'Ocean Breeze', 'wppowersuite-lite' ),
				'description' => __( 'Coastal photo with airy spacing, pill fields, and teal gradient button.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'split-editorial',
				'group'       => 'split',
				'name'        => __( 'Editorial Split', 'wppowersuite-lite' ),
				'description' => __( 'White form column beside a navy hero with serif welcome copy.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'split-slate',
				'group'       => 'split',
				'name'        => __( 'Slate Studio', 'wppowersuite-lite' ),
				'description' => __( 'Balanced 50/50 split with cool slate hero and refined form styling.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'split-horizon',
				'group'       => 'split',
				'name'        => __( 'Color Horizon', 'wppowersuite-lite' ),
				'description' => __( 'Violet-to-blue hero with a crisp white form and gradient sign-in.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'split-workspace',
				'group'       => 'split',
				'name'        => __( 'Modern Workspace', 'wppowersuite-lite' ),
				'description' => __( 'Office photography on the hero side with a light, professional form panel.', 'wppowersuite-lite' ),
			],
			[
				'id'          => 'split-creative',
				'group'       => 'split',
				'name'        => __( 'Creative Studio', 'wppowersuite-lite' ),
				'description' => __( 'Abstract art hero with bold magenta accents and a minimal white form.', 'wppowersuite-lite' ),
			],
		];
	}

	/**
	 * Thumbnail spec derived from preset settings so admin previews match the live layout.
	 *
	 * @return array<string, mixed>
	 */
	public static function getPreviewSpec( string $preset_id ): array {
		$settings = self::getPresetSettings( $preset_id );
		if ( $settings === null ) {
			return [];
		}

		$template = isset( $settings['template_type'] ) ? (string) $settings['template_type'] : 'classic-centered';
		if ( $template === 'split-layout' ) {
			return self::buildSplitPreviewSpec( $settings );
		}

		return self::buildClassicPreviewSpec( $settings );
	}

	/** @param array<string, mixed> $settings @return array<string, mixed> */
	private static function buildClassicPreviewSpec( array $settings ): array {
		$page_bg     = self::resolveBackgroundLayer( $settings, 'classic_bg', '#f6f8fc' );
		$box_fill    = self::resolveSolidColor( $settings['box_bg_color'] ?? '#ffffff', '#ffffff' );
		$field_fill  = self::resolveSolidColor( $settings['field_normal_bg_color'] ?? '#ffffff', '#ffffff' );
		$field_border = self::resolveSolidColor( $settings['field_normal_border_color'] ?? '#c3c4c7', '#c3c4c7' );
		$button_fill = self::resolveButtonFill( $settings );
		$box_radius  = self::resolveRadius( $settings['box_border_radius_desktop'] ?? '12' );
		$field_radius = self::resolveRadius( $settings['field_normal_border_radius_desktop'] ?? '6' );
		$button_radius = self::resolveRadius( $settings['button_normal_border_radius_desktop'] ?? '6' );

		return [
			'layout'         => 'centered',
			'page_fill'      => $page_bg['fill'],
			'page_image'     => $page_bg['image'],
			'page_overlay'   => $page_bg['overlay'],
			'box_fill'       => $box_fill,
			'box_radius'     => $box_radius,
			'show_box_card'  => true,
			'field_fill'     => $field_fill,
			'field_border'   => $field_border,
			'field_radius'   => $field_radius,
			'button_fill'    => $button_fill,
			'button_radius'  => $button_radius,
			'accent'         => $button_fill,
			'has_photo'      => $page_bg['image'] !== '',
			'hero_content'   => false,
		];
	}

	/** @param array<string, mixed> $settings @return array<string, mixed> */
	private static function buildSplitPreviewSpec( array $settings ): array {
		$form_pct = isset( $settings['split_ratio'] ) && in_array( (string) $settings['split_ratio'], [ '25', '30', '35', '40', '45', '50' ], true )
			? (int) $settings['split_ratio']
			: 50;
		$column_order = ( isset( $settings['split_column_order'] ) && (string) $settings['split_column_order'] === 'form-second' )
			? 'form-second'
			: 'form-first';

		$form_bg  = self::resolveBackgroundLayer( $settings, 'split_form_bg', '#ffffff' );
		$hero_bg  = self::resolveBackgroundLayer( $settings, 'split_empty_bg', '#f0f0f1' );
		$box_fill = self::resolveSolidColor( $settings['box_bg_color'] ?? '#ffffff', '#ffffff' );

		return [
			'layout'         => 'split',
			'form_pct'       => $form_pct,
			'column_order'   => $column_order,
			'form_fill'      => $form_bg['fill'],
			'form_image'     => $form_bg['image'],
			'form_overlay'   => $form_bg['overlay'],
			'hero_fill'      => $hero_bg['fill'],
			'hero_image'     => $hero_bg['image'],
			'hero_overlay'   => $hero_bg['overlay'],
			'box_fill'       => $box_fill,
			'box_radius'     => self::resolveRadius( $settings['box_border_radius_desktop'] ?? '12' ),
			'show_box_card'  => true,
			'field_fill'     => self::resolveSolidColor( $settings['field_normal_bg_color'] ?? '#ffffff', '#ffffff' ),
			'field_border'   => self::resolveSolidColor( $settings['field_normal_border_color'] ?? '#c3c4c7', '#c3c4c7' ),
			'field_radius'   => self::resolveRadius( $settings['field_normal_border_radius_desktop'] ?? '6' ),
			'button_fill'    => self::resolveButtonFill( $settings ),
			'button_radius'  => self::resolveRadius( $settings['button_normal_border_radius_desktop'] ?? '6' ),
			'accent'         => self::resolveButtonFill( $settings ),
			'has_photo'      => $form_bg['image'] !== '' || $hero_bg['image'] !== '',
			'hero_content'   => false,
		];
	}

	/**
	 * @param array<string, mixed> $settings
	 * @return array{fill: string, image: string, overlay: string}
	 */
	private static function resolveBackgroundLayer( array $settings, string $prefix, string $fallback ): array {
		$type = isset( $settings[ $prefix . '_type' ] ) ? trim( (string) $settings[ $prefix . '_type' ] ) : '';

		if ( $type === 'color' ) {
			return [
				'fill'    => self::resolveSolidColor( $settings[ $prefix . '_color' ] ?? $fallback, $fallback ),
				'image'   => '',
				'overlay' => '',
			];
		}

		if ( $type === 'gradient' ) {
			$gradient = self::gradientToCss( (string) ( $settings[ $prefix . '_gradient' ] ?? '' ) );

			return [
				'fill'    => $gradient !== '' ? $gradient : $fallback,
				'image'   => '',
				'overlay' => '',
			];
		}

		if ( $type === 'image' ) {
			$url = isset( $settings[ $prefix . '_image' ] ) ? trim( (string) $settings[ $prefix . '_image' ] ) : '';
			if ( $url === '' ) {
				return [
					'fill'    => $fallback,
					'image'   => '',
					'overlay' => '',
				];
			}

			$overlay_color   = self::resolveSolidColor( $settings[ $prefix . '_image_overlay_color' ] ?? '#000000', '#000000' );
			$overlay_opacity = isset( $settings[ $prefix . '_image_overlay_opacity' ] ) ? (int) $settings[ $prefix . '_image_overlay_opacity'] : 0;
			$overlay_opacity = max( 0, min( 100, $overlay_opacity ) );

			return [
				'fill'    => $overlay_color,
				'image'   => $url,
				'overlay' => $overlay_opacity > 0 ? self::hexToRgba( $overlay_color, $overlay_opacity / 100 ) : '',
			];
		}

		return [
			'fill'    => $fallback,
			'image'   => '',
			'overlay' => '',
		];
	}

	/** @param array<string, mixed> $settings */
	private static function resolveButtonFill( array $settings ): string {
		$type = isset( $settings['button_normal_bg_type'] ) ? trim( (string) $settings['button_normal_bg_type'] ) : '';
		if ( $type === 'gradient' ) {
			$gradient = self::gradientToCss( (string) ( $settings['button_normal_bg_gradient'] ?? '' ) );
			if ( $gradient !== '' ) {
				return $gradient;
			}
		}

		return self::resolveSolidColor( $settings['button_normal_bg_color'] ?? '#2271b1', '#2271b1' );
	}

	private static function gradientToCss( string $json ): string {
		if ( $json === '' ) {
			return '';
		}

		$decoded = json_decode( $json, true );
		if ( ! is_array( $decoded ) || empty( $decoded['stops'] ) || ! is_array( $decoded['stops'] ) ) {
			return '';
		}

		$angle = isset( $decoded['angle'] ) ? max( 0, min( 360, (int) $decoded['angle'] ) ) : 135;
		$stops = [];
		foreach ( $decoded['stops'] as $stop ) {
			if ( ! is_array( $stop ) ) {
				continue;
			}
			$color = isset( $stop['color'] ) ? (string) $stop['color'] : '#ffffff';
			$pos   = isset( $stop['pos'] ) ? max( 0, min( 100, (int) $stop['pos'] ) ) : 0;
			$stops[] = $color . ' ' . $pos . '%';
		}

		if ( $stops === [] ) {
			return '';
		}

		return 'linear-gradient(' . $angle . 'deg,' . implode( ',', $stops ) . ')';
	}

	/** @param mixed $value */
	private static function resolveSolidColor( $value, string $fallback ): string {
		$color = is_string( $value ) ? trim( $value ) : '';
		if ( $color === '' ) {
			return $fallback;
		}

		if ( preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/', $color ) ) {
			return $color;
		}

		return $fallback;
	}

	/** @param mixed $value */
	private static function resolveRadius( $value ): string {
		$radius = is_numeric( $value ) ? (int) $value : 6;

		return (string) max( 0, min( 999, $radius ) );
	}

	private static function hexToRgba( string $hex, float $alpha ): string {
		$hex = ltrim( $hex, '#' );
		if ( strlen( $hex ) === 3 ) {
			$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
		}

		$r = hexdec( substr( $hex, 0, 2 ) );
		$g = hexdec( substr( $hex, 2, 2 ) );
		$b = hexdec( substr( $hex, 4, 2 ) );

		return 'rgba(' . $r . ',' . $g . ',' . $b . ',' . max( 0, min( 1, $alpha ) ) . ')';
	}

	/** @return array<string, array<string, mixed>> */
	private static function getOverrides(): array {
		$forest_img   = self::imageUrl( 'photo-1441974231531-c6227db76b6e' );
		$city_img     = self::imageUrl( 'photo-1514565131-fce0801e5785' );
		$ocean_img    = self::imageUrl( 'photo-1505142468610-359e7d316be0' );
		$workspace_img = self::imageUrl( 'photo-1497366811353-6870744d04b2' );
		$creative_img = self::imageUrl( 'photo-1558591710-4b4a1ae0f04d' );
		$split_base   = self::splitContainerStyles();

		return [
			'default' => [],
			'classic-clean' => [
				'layout_preset'                        => 'classic-clean',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'color',
				'classic_bg_color'                     => '#f4f4f5',
				'classic_bg_image'                     => '',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '360',
				'box_padding_top'                      => '28',
				'box_padding_right'                    => '32',
				'box_padding_bottom'                   => '28',
				'box_padding_left'                     => '32',
				'box_border_radius_desktop'            => '10',
				'box_shadow_y'                         => 6,
				'box_shadow_blur'                      => 18,
				'box_shadow_color'                     => '#00000012',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#e4e4e7',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '6',
				'field_normal_bg_color'                => '#ffffff',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#3f3f46',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '6',
				'button_hover_bg_color'                => '#27272a',
			],
			'classic-night' => [
				'layout_preset'                        => 'classic-night',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'color',
				'classic_bg_color'                     => '#0f172a',
				'classic_bg_image'                     => '',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '400',
				'box_bg_type'                          => 'color',
				'box_bg_color'                         => '#ffffff',
				'box_padding_top'                      => '36',
				'box_padding_right'                    => '40',
				'box_padding_bottom'                   => '36',
				'box_padding_left'                     => '40',
				'box_border_radius_desktop'            => '16',
				'box_shadow_y'                         => 16,
				'box_shadow_blur'                      => 48,
				'box_shadow_color'                     => '#00000055',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#cbd5e1',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '8',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#0ea5e9',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '8',
				'button_hover_bg_color'                => '#0284c7',
			],
			'classic-sunrise' => [
				'layout_preset'                        => 'classic-sunrise',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'gradient',
				'classic_bg_gradient'                  => '{"type":"linear","angle":145,"stops":[{"color":"#fff7ed","pos":0},{"color":"#fed7aa","pos":100}]}',
				'classic_bg_image'                     => '',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '400',
				'box_border_radius_desktop'            => '16',
				'box_shadow_y'                         => 12,
				'box_shadow_blur'                      => 32,
				'box_shadow_color'                     => '#ea580c22',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#fdba74',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '10',
				'field_normal_bg_color'                => '#fffbeb',
				'button_normal_bg_type'                => 'gradient',
				'button_normal_bg_gradient'            => '{"type":"linear","angle":135,"stops":[{"color":"#fb923c","pos":0},{"color":"#ea580c","pos":100}]}',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '10',
				'button_hover_bg_type'                 => 'color',
				'button_hover_bg_color'                => '#c2410c',
			],
			'classic-forest' => [
				'layout_preset'                        => 'classic-forest',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'image',
				'classic_bg_image'                     => $forest_img,
				'classic_bg_image_overlay_color'       => '#0f172a',
				'classic_bg_image_overlay_opacity'     => '40',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '400',
				'box_border_radius_desktop'            => '14',
				'box_shadow_y'                         => 20,
				'box_shadow_blur'                      => 50,
				'box_shadow_color'                     => '#00000040',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#bbf7d0',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '8',
				'field_normal_bg_color'                => '#f0fdf4',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#16a34a',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '8',
				'button_hover_bg_color'                => '#15803d',
			],
			'classic-glass' => [
				'layout_preset'                        => 'classic-glass',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'image',
				'classic_bg_image'                     => $city_img,
				'classic_bg_image_overlay_color'       => '#1e1b4b',
				'classic_bg_image_overlay_opacity'     => '50',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '400',
				'box_bg_color'                         => '#ffffff',
				'box_border_radius_desktop'            => '18',
				'box_shadow_y'                         => 24,
				'box_shadow_blur'                      => 60,
				'box_shadow_color'                     => '#00000050',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#c7d2fe',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '10',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#4f46e5',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '10',
				'button_hover_bg_color'                => '#4338ca',
			],
			'classic-ocean' => [
				'layout_preset'                        => 'classic-ocean',
				'template_type'                        => 'classic-centered',
				'form_alignment'                       => 'center',
				'classic_bg_type'                      => 'image',
				'classic_bg_image'                     => $ocean_img,
				'classic_bg_image_overlay_color'       => '#0f766e',
				'classic_bg_image_overlay_opacity'     => '30',
				'classic_bg_video'                     => '',
				'box_form_width'                       => '400',
				'box_border_radius_desktop'            => '20',
				'box_shadow_y'                         => 14,
				'box_shadow_blur'                      => 40,
				'box_shadow_color'                     => '#0d948833',
				'field_normal_border_type'             => 'solid',
				'field_normal_border_color'            => '#99f6e4',
				'field_normal_border_width'            => '1',
				'field_normal_border_radius_desktop'   => '999',
				'field_normal_bg_color'                => '#f0fdfa',
				'button_normal_bg_type'                => 'gradient',
				'button_normal_bg_gradient'            => '{"type":"linear","angle":90,"stops":[{"color":"#14b8a6","pos":0},{"color":"#0d9488","pos":100}]}',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '999',
				'button_hover_bg_type'                 => 'color',
				'button_hover_bg_color'                => '#0f766e',
			],
			'split-editorial' => array_merge( $split_base, [
				'layout_preset'                        => 'split-editorial',
				'template_type'                        => 'split-layout',
				'split_ratio'                          => '40',
				'split_column_order'                   => 'form-first',
				'split_form_bg_type'                   => 'color',
				'split_form_bg_color'                  => '#f6f8fc',
				'split_form_bg_image'                  => '',
				'split_form_bg_video'                  => '',
				'split_empty_bg_type'                  => 'gradient',
				'split_empty_bg_gradient'              => '{"type":"linear","angle":160,"stops":[{"color":"#1e3a5f","pos":0},{"color":"#0f172a","pos":100}]}',
				'split_empty_add_content'              => '1',
				'split_empty_content'                  => '<h2 style="font-family: Georgia, serif; font-size: 32px; font-weight: 700; margin: 0 0 16px; color: #f8fafc; line-height: 1.2;">' . "\n" . esc_html__( 'Welcome back', 'wppowersuite-lite' ) . "\n" . '</h2>' . "\n" . '<p style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 16px; line-height: 1.7; margin: 0; color: #cbd5e1; max-width: 36ch;">' . "\n" . esc_html__( 'Sign in to manage your site. This editorial split keeps focus on the form while your brand story lives beside it.', 'wppowersuite-lite' ) . "\n" . '</p>',
				'box_form_width'                       => '360',
				'field_normal_border_color'            => '#dbeafe',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#2563eb',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '6',
				'button_hover_bg_color'                => '#1d4ed8',
			] ),
			'split-slate' => array_merge( $split_base, [
				'layout_preset'                        => 'split-slate',
				'template_type'                        => 'split-layout',
				'split_ratio'                          => '50',
				'split_column_order'                   => 'form-first',
				'split_form_bg_type'                   => 'color',
				'split_form_bg_color'                  => '#f8fafc',
				'split_form_bg_image'                  => '',
				'split_form_bg_video'                  => '',
				'split_empty_bg_type'                  => 'gradient',
				'split_empty_bg_gradient'              => '{"type":"linear","angle":145,"stops":[{"color":"#475569","pos":0},{"color":"#1e293b","pos":100}]}',
				'split_empty_add_content'              => '1',
				'split_empty_content'                  => '<h2 style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 28px; font-weight: 600; margin: 0 0 12px; color: #f1f5f9;">' . "\n" . esc_html__( 'Your workspace', 'wppowersuite-lite' ) . "\n" . '</h2>' . "\n" . '<p style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 15px; line-height: 1.65; margin: 0; color: #94a3b8;">' . "\n" . esc_html__( 'A balanced split for teams who want a professional, understated login experience.', 'wppowersuite-lite' ) . "\n" . '</p>',
				'box_form_width'                       => '380',
				'field_normal_border_color'            => '#cbd5e1',
				'field_normal_border_radius_desktop'   => '8',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#334155',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '8',
				'button_hover_bg_color'                => '#1e293b',
			] ),
			'split-horizon' => array_merge( $split_base, [
				'layout_preset'                        => 'split-horizon',
				'template_type'                        => 'split-layout',
				'split_ratio'                          => '35',
				'split_column_order'                   => 'form-second',
				'split_form_bg_type'                   => 'color',
				'split_form_bg_color'                  => '#f8fafc',
				'split_form_bg_image'                  => '',
				'split_form_bg_video'                  => '',
				'split_empty_bg_type'                  => 'gradient',
				'split_empty_bg_gradient'              => '{"type":"linear","angle":135,"stops":[{"color":"#2563eb","pos":0},{"color":"#7c3aed","pos":100}]}',
				'split_empty_add_content'              => '1',
				'split_empty_content'                  => '<h2 style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 30px; font-weight: 700; margin: 0 0 14px; color: #ffffff;">' . "\n" . esc_html__( 'Hello there', 'wppowersuite-lite' ) . "\n" . '</h2>' . "\n" . '<p style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 16px; line-height: 1.7; margin: 0; color: #e9d5ff;">' . "\n" . esc_html__( 'Vibrant color-block split for modern brands that are not afraid of bold first impressions.', 'wppowersuite-lite' ) . "\n" . '</p>',
				'box_form_width'                       => '380',
				'box_border_radius_desktop'            => '14',
				'box_shadow_y'                         => 10,
				'box_shadow_blur'                      => 30,
				'field_normal_border_color'            => '#ddd6fe',
				'field_normal_border_radius_desktop'   => '8',
				'button_normal_bg_type'                => 'gradient',
				'button_normal_bg_gradient'            => '{"type":"linear","angle":90,"stops":[{"color":"#7c3aed","pos":0},{"color":"#2563eb","pos":100}]}',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '8',
				'button_hover_bg_type'                 => 'color',
				'button_hover_bg_color'                => '#5b21b6',
			] ),
			'split-workspace' => array_merge( $split_base, [
				'layout_preset'                        => 'split-workspace',
				'template_type'                        => 'split-layout',
				'split_ratio'                          => '45',
				'split_column_order'                   => 'form-first',
				'split_form_bg_type'                   => 'color',
				'split_form_bg_color'                  => '#f8fafc',
				'split_form_bg_image'                  => '',
				'split_form_bg_video'                  => '',
				'split_empty_bg_type'                  => 'image',
				'split_empty_bg_image'                 => $workspace_img,
				'split_empty_bg_image_overlay_color'   => '#0f172a',
				'split_empty_bg_image_overlay_opacity' => '45',
				'split_empty_add_content'              => '1',
				'split_empty_content'                  => '<h2 style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 30px; font-weight: 700; margin: 0 0 14px; color: #ffffff;">' . "\n" . esc_html__( 'Work smarter', 'wppowersuite-lite' ) . "\n" . '</h2>' . "\n" . '<p style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 16px; line-height: 1.7; margin: 0; color: #e2e8f0;">' . "\n" . esc_html__( 'A bright form column paired with real workspace photography for SaaS and agency brands.', 'wppowersuite-lite' ) . "\n" . '</p>',
				'box_form_width'                       => '380',
				'field_normal_border_color'            => '#cbd5e1',
				'field_normal_border_radius_desktop'   => '8',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#0f766e',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '8',
				'button_hover_bg_color'                => '#115e59',
			] ),
			'split-creative' => array_merge( $split_base, [
				'layout_preset'                        => 'split-creative',
				'template_type'                        => 'split-layout',
				'split_ratio'                          => '38',
				'split_column_order'                   => 'form-second',
				'split_form_bg_type'                   => 'color',
				'split_form_bg_color'                  => '#f8fafc',
				'split_form_bg_image'                  => '',
				'split_form_bg_video'                  => '',
				'split_empty_bg_type'                  => 'image',
				'split_empty_bg_image'                 => $creative_img,
				'split_empty_bg_image_overlay_color'   => '#831843',
				'split_empty_bg_image_overlay_opacity' => '50',
				'split_empty_add_content'              => '1',
				'split_empty_content'                  => '<h2 style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 32px; font-weight: 800; margin: 0 0 14px; color: #ffffff; letter-spacing: -0.02em;">' . "\n" . esc_html__( 'Create boldly', 'wppowersuite-lite' ) . "\n" . '</h2>' . "\n" . '<p style="font-family: -apple-system, BlinkMacSystemFont, Segoe UI, sans-serif; font-size: 16px; line-height: 1.7; margin: 0; color: #fce7f3;">' . "\n" . esc_html__( 'Abstract art hero with a clean white form — ideal for studios, portfolios, and creative teams.', 'wppowersuite-lite' ) . "\n" . '</p>',
				'box_form_width'                       => '380',
				'box_border_radius_desktop'            => '16',
				'field_normal_border_color'            => '#fbcfe8',
				'field_normal_border_radius_desktop'   => '10',
				'field_normal_bg_color'                => '#fdf2f8',
				'button_normal_bg_type'                => 'color',
				'button_normal_bg_color'               => '#db2777',
				'button_normal_text_color'             => '#ffffff',
				'button_normal_border_radius_desktop'  => '10',
				'button_hover_bg_color'                => '#be185d',
			] ),
		];
	}

	/**
	 * Full settings array for a preset (defaults merged with overrides).
	 *
	 * @return array<string, mixed>|null
	 */
	public static function getPresetSettings( string $preset_id ): ?array {
		$overrides = self::getOverrides();
		if ( ! isset( $overrides[ $preset_id ] ) ) {
			return null;
		}

		$settings = array_merge( Module::getDefaultSettings(), self::responsivePresetTweaks(), $overrides[ $preset_id ] );
		if ( $preset_id === 'default' ) {
			$settings['layout_preset'] = 'default';
		}

		$settings = self::expandGradientFields( $settings );

		$normal_text = isset( $settings['button_normal_text_color'] ) ? trim( (string) $settings['button_normal_text_color'] ) : '';
		$hover_text  = isset( $settings['button_hover_text_color'] ) ? trim( (string) $settings['button_hover_text_color'] ) : '';
		if ( $hover_text === '' && $normal_text !== '' ) {
			$settings['button_hover_text_color'] = $normal_text;
		}

		return $settings;
	}

	/**
	 * Presets keyed by ID for admin JavaScript.
	 *
	 * @return array<string, array<string, mixed>>
	 */
	public static function getPresetsForJs(): array {
		$out = [];
		foreach ( array_keys( self::getOverrides() ) as $preset_id ) {
			$settings = self::getPresetSettings( $preset_id );
			if ( $settings !== null ) {
				$out[ $preset_id ] = $settings;
			}
		}
		return $out;
	}

	/**
	 * Add flat gradient editor fields expected by the admin form.
	 *
	 * @param array<string, mixed> $settings
	 * @return array<string, mixed>
	 */
	private static function expandGradientFields( array $settings ): array {
		$map = [
			'classic_bg_gradient'        => 'classic_bg',
			'split_form_bg_gradient'     => 'split_form_bg',
			'split_empty_bg_gradient'    => 'split_empty_bg',
			'box_bg_gradient'            => 'box_bg',
			'form_bg_gradient'           => 'form_bg',
			'button_normal_bg_gradient'  => 'button_normal_bg',
			'button_hover_bg_gradient'   => 'button_hover_bg',
		];

		foreach ( $map as $json_key => $prefix ) {
			$grad = AdminHelpers::parseGradient( $settings, $json_key );
			$settings[ $prefix . '_gradient_type' ]   = $grad['type'];
			$settings[ $prefix . '_gradient_angle' ]  = (string) $grad['angle'];
			$settings[ $prefix . '_gradient_color1' ]  = $grad['color1'];
			$settings[ $prefix . '_gradient_pos1' ]    = (string) $grad['pos1'];
			$settings[ $prefix . '_gradient_color2' ]  = $grad['color2'];
			$settings[ $prefix . '_gradient_pos2' ]    = (string) $grad['pos2'];
		}

		return $settings;
	}
}
