<?php
/**
 * Per-section design controls – padding, backgrounds, responsive overrides.
 *
 * @package EasySite_Pro
 */

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

/**
 * Default design options for a block.
 *
 * @return array
 */
function easysite_get_default_block_design() {
	return array(
		'padding_top'    => 'normal',
		'padding_bottom' => 'normal',
		'bg_color'       => '',
		'bg_image'       => '',
		'text_color'     => '',
		'border_radius'  => 'none',
		'tablet'         => array(),
		'mobile'         => array(),
	);
}

/**
 * Design field definitions for the visual editor panel.
 *
 * @return array
 */
function easysite_get_design_field_defs() {
	return array(
		'padding_top'    => array(
			'label'   => __( 'Padding top', 'easysite-pro' ),
			'type'    => 'select',
			'options' => easysite_get_spacing_options(),
		),
		'padding_bottom' => array(
			'label'   => __( 'Padding bottom', 'easysite-pro' ),
			'type'    => 'select',
			'options' => easysite_get_spacing_options(),
		),
		'bg_color'       => array(
			'label' => __( 'Background color', 'easysite-pro' ),
			'type'  => 'color',
		),
		'bg_image'       => array(
			'label' => __( 'Background image', 'easysite-pro' ),
			'type'  => 'image',
		),
		'text_color'     => array(
			'label' => __( 'Text color', 'easysite-pro' ),
			'type'  => 'color',
		),
		'border_radius'  => array(
			'label'   => __( 'Rounded corners', 'easysite-pro' ),
			'type'    => 'select',
			'options' => array(
				'none'   => __( 'None', 'easysite-pro' ),
				'small'  => __( 'Small', 'easysite-pro' ),
				'medium' => __( 'Medium', 'easysite-pro' ),
				'large'  => __( 'Large', 'easysite-pro' ),
			),
		),
	);
}

/**
 * Spacing size options.
 *
 * @return array
 */
function easysite_get_spacing_options() {
	return array(
		'none'   => __( 'None', 'easysite-pro' ),
		'small'  => __( 'Small', 'easysite-pro' ),
		'normal' => __( 'Normal', 'easysite-pro' ),
		'large'  => __( 'Large', 'easysite-pro' ),
	);
}

/**
 * Padding values map.
 *
 * @return array
 */
function easysite_get_spacing_map() {
	return array(
		'none'   => '0',
		'small'  => '2rem',
		'normal' => '4rem',
		'large'  => '6rem',
	);
}

/**
 * Border radius map.
 *
 * @return array
 */
function easysite_get_radius_map() {
	return array(
		'none'   => '0',
		'small'  => '8px',
		'medium' => '16px',
		'large'  => '24px',
	);
}

/**
 * Get merged design for a block.
 *
 * @param array $block Block data.
 * @return array
 */
function easysite_get_block_design( $block ) {
	$defaults = easysite_get_default_block_design();
	$design   = isset( $block['design'] ) && is_array( $block['design'] ) ? $block['design'] : array();
	return wp_parse_args( $design, $defaults );
}

/**
 * Sanitize block design array.
 *
 * @param array $design Raw design.
 * @return array
 */
function easysite_sanitize_block_design( $design ) {
	if ( ! is_array( $design ) ) {
		return easysite_get_default_block_design();
	}

	$spacing_keys = array_keys( easysite_get_spacing_options() );
	$radius_keys  = array_keys( easysite_get_radius_map() );
	$clean        = easysite_get_default_block_design();

	foreach ( array( 'padding_top', 'padding_bottom', 'border_radius' ) as $key ) {
		if ( isset( $design[ $key ] ) && in_array( $design[ $key ], $spacing_keys, true ) ) {
			$clean[ $key ] = $design[ $key ];
		} elseif ( 'border_radius' === $key && isset( $design[ $key ] ) && in_array( $design[ $key ], $radius_keys, true ) ) {
			$clean[ $key ] = $design[ $key ];
		}
	}

	if ( ! empty( $design['bg_color'] ) ) {
		$clean['bg_color'] = easysite_sanitize_hex_color( $design['bg_color'] ) ?: '';
	}
	if ( ! empty( $design['text_color'] ) ) {
		$clean['text_color'] = easysite_sanitize_hex_color( $design['text_color'] ) ?: '';
	}
	if ( ! empty( $design['bg_image'] ) ) {
		$clean['bg_image'] = esc_url_raw( $design['bg_image'] );
	}

	foreach ( array( 'tablet', 'mobile' ) as $bp ) {
		if ( empty( $design[ $bp ] ) || ! is_array( $design[ $bp ] ) ) {
			continue;
		}
		$bp_clean = array();
		foreach ( array( 'padding_top', 'padding_bottom' ) as $key ) {
			if ( isset( $design[ $bp ][ $key ] ) && in_array( $design[ $bp ][ $key ], $spacing_keys, true ) ) {
				$bp_clean[ $key ] = $design[ $bp ][ $key ];
			}
		}
		if ( ! empty( $design[ $bp ]['text_color'] ) ) {
			$bp_clean['text_color'] = easysite_sanitize_hex_color( $design[ $bp ]['text_color'] ) ?: '';
		}
		if ( ! empty( $design[ $bp ]['hide'] ) ) {
			$bp_clean['hide'] = true;
		}
		$clean[ $bp ] = $bp_clean;
	}

	return $clean;
}

/**
 * Build inline style for block design at a breakpoint.
 *
 * @param array  $block      Block data.
 * @param string $breakpoint desktop|tablet|mobile.
 * @return string
 */
function easysite_block_design_style( $block, $breakpoint = 'desktop' ) {
	$design  = easysite_get_block_design( $block );
	$spacing = easysite_get_spacing_map();
	$radius  = easysite_get_radius_map();

	$vals = $design;
	if ( 'tablet' === $breakpoint && ! empty( $design['tablet'] ) ) {
		$vals = array_merge( $vals, $design['tablet'] );
	}
	if ( 'mobile' === $breakpoint && ! empty( $design['mobile'] ) ) {
		$vals = array_merge( $vals, $design['mobile'] );
	}

	if ( ! empty( $vals['hide'] ) && 'desktop' !== $breakpoint ) {
		return 'display:none;';
	}

	$styles = array();

	$pt = $vals['padding_top'] ?? 'normal';
	$pb = $vals['padding_bottom'] ?? 'normal';
	if ( isset( $spacing[ $pt ] ) ) {
		$styles[] = 'padding-top:' . $spacing[ $pt ];
	}
	if ( isset( $spacing[ $pb ] ) ) {
		$styles[] = 'padding-bottom:' . $spacing[ $pb ];
	}

	if ( ! empty( $vals['bg_color'] ) ) {
		$styles[] = 'background-color:' . $vals['bg_color'];
	}
	if ( ! empty( $vals['bg_image'] ) ) {
		$styles[] = "background-image:url('" . esc_url( $vals['bg_image'] ) . "')";
		$styles[] = 'background-size:cover';
		$styles[] = 'background-position:center';
	}
	if ( ! empty( $vals['text_color'] ) ) {
		$styles[] = 'color:' . $vals['text_color'];
	}

	$br = $vals['border_radius'] ?? 'none';
	if ( isset( $radius[ $br ] ) && 'none' !== $br ) {
		$styles[] = 'border-radius:' . $radius[ $br ];
		$styles[] = 'overflow:hidden';
	}

	return implode( ';', $styles ) . ( $styles ? ';' : '' );
}

/**
 * CSS custom properties + classes for responsive design overrides.
 *
 * @param array $block Block.
 * @return string HTML attributes.
 */
function easysite_block_design_attrs( $block ) {
	$design = easysite_get_block_design( $block );
	$attrs  = ' data-esb-design="1"';

	$desktop = easysite_block_design_style( $block, 'desktop' );
	if ( $desktop ) {
		$attrs .= ' style="' . esc_attr( $desktop ) . '"';
	}

	if ( ! empty( $design['tablet'] ) ) {
		$attrs .= ' data-design-tablet="' . esc_attr( wp_json_encode( $design['tablet'] ) ) . '"';
	}
	if ( ! empty( $design['mobile'] ) ) {
		$attrs .= ' data-design-mobile="' . esc_attr( wp_json_encode( $design['mobile'] ) ) . '"';
	}

	return $attrs;
}

/**
 * Output HTML content (rich text or plain).
 *
 * @param string $content Field content.
 * @return string Safe HTML.
 */
function easysite_pb_output_html( $content ) {
	if ( ! $content ) {
		return '';
	}
	if ( $content !== strip_tags( $content ) ) {
		return wp_kses_post( $content );
	}
	return nl2br( esc_html( $content ) );
}

/**
 * Allowed kses tags for rich text fields.
 *
 * @return array
 */
function easysite_ve_kses_allowed() {
	return array(
		'a'      => array( 'href' => true, 'title' => true, 'target' => true, 'rel' => true ),
		'br'     => array(),
		'em'     => array(),
		'strong' => array(),
		'b'      => array(),
		'i'      => array(),
		'u'      => array(),
		'p'      => array(),
		'h2'     => array(),
		'h3'     => array(),
		'h4'     => array(),
		'span'   => array( 'style' => true ),
	);
}

/**
 * Sanitize rich text field value.
 *
 * @param string $val Raw value.
 * @return string
 */
function easysite_sanitize_richtext( $val ) {
	return wp_kses( $val, easysite_ve_kses_allowed() );
}

/**
 * Rich-text editable attribute.
 *
 * @param string $field Field key.
 * @return string
 */
function easysite_ve_richtext_attr( $field ) {
	if ( ! easysite_ve_active() ) {
		return '';
	}
	return ' data-esb-field="' . esc_attr( $field ) . '" data-esb-type="richtext" contenteditable="true" spellcheck="true"';
}
