<?php

/**
 * Rendu serveur : évite de persister du SVG/styles dans le post
 * (filtrés par KSES pour les rôles sans unfiltered_html).
 *
 * @var array    $attributes Attributs du bloc.
 * @var string   $content    HTML des blocs internes (InnerBlocks).
 * @var WP_Block $block      Instance du bloc.
 */

$has_light_background = ! empty($attributes['hasLightBackground']);
$disposition          = $attributes['disposition'] ?? 'left';
$cover_url            = $attributes['coverUrl'] ?? '';
$cover_alt            = $attributes['coverAlt'] ?? '';
$cover_size           = $attributes['coverSize'] ?? 'large';
$cover_type           = $attributes['coverType'] ?? 'image';
$background_color     = $attributes['backgroundColor'] ?? '#ffffff';
$text_color           = $attributes['textColor'] ?? '#136f63';
$shape_type           = $attributes['shapeType'] ?? 'variationA';
$block_variant        = $attributes['blockVariant'] ?? 'framed';
$border_color         = $attributes['borderColor'] ?? '#136f63';

// Ancien contenu statique (save.js complet) : HTML déjà prêt, ne pas re-envelopper.
$is_legacy_static = $content && (
	str_contains($content, 'deligraph-blocks-chapter-section')
	|| str_contains($content, 'chapter-section__inner')
);

if ($is_legacy_static) {
	echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	return;
}

$bg_light_class = $has_light_background ? 'chapter-section--bg-light' : 'chapter-section--bg-dark';

$wrapper_attributes = get_block_wrapper_attributes(
	array(
		'class' => sprintf(
			'deligraph-blocks-chapter-section chapter-section chapter-section--%s chapter-section--%s %s',
			esc_attr($disposition),
			esc_attr($block_variant),
			esc_attr($bg_light_class)
		),
		'style' => sprintf(
			'--chapter-section-text-color:%s;--cta-current-color:%s',
			esc_attr($text_color ? $text_color : 'var(--advised-text-color)'),
			esc_attr($block_variant === 'backgrounded' ? 'inherit' : 'var(--wp--preset--color--primary) !important')
		),
	),
	isset($block) ? $block : null
);

$show_shape_a = ($block_variant === 'backgrounded' || $block_variant === 'framed')
	&& $background_color
	&& $shape_type === 'variationA';
$show_shape_b = ($block_variant === 'backgrounded' || $block_variant === 'framed')
	&& $background_color
	&& $shape_type === 'variationB';

$shape_fill   = $block_variant === 'backgrounded' ? esc_attr($background_color) : 'none';
$shape_stroke = $block_variant === 'framed' ? esc_attr($border_color) : 'none';
$stroke_width = ($block_variant === 'framed' && $border_color) ? '2px' : '0';
?>
<section <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
			?>>
	<div class="chapter-section__inner">
		<?php if ($show_shape_a) : ?>
			<svg width="1440" height="744" viewBox="0 0 1440 744" fill="none" xmlns="http://www.w3.org/2000/svg" class="chapter-section__background" preserveAspectRatio="none" aria-hidden="true" focusable="false">
				<path d="M0 0H1440V686.701L0 744V0Z" fill="<?php echo $shape_fill; ?>" stroke="<?php echo $shape_stroke; ?>" stroke-width="<?php echo esc_attr($stroke_width); ?>" style="stroke-linejoin:round"></path>
			</svg>
		<?php endif; ?>
		<?php if ($show_shape_b) : ?>
			<svg class="chapter-section__background" width="1302" height="654" viewBox="0 0 1302 654" preserveAspectRatio="none" aria-hidden="true" focusable="false">
				<path d="M1302 0L0 15.8281V654L1302 642.633L1302 0Z" fill="<?php echo $shape_fill; ?>" stroke="<?php echo $shape_stroke; ?>" stroke-width="<?php echo esc_attr($stroke_width); ?>" style="stroke-linejoin:round"></path>
			</svg>
		<?php endif; ?>
		<div class="chapter-section__content">
			<div class="chapter-section__innerblocks">
				<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
				?>
			</div>
		</div>
		<?php if ($cover_url) : ?>
			<img
				class="<?php echo esc_attr("chapter-section__cover chapter-section__cover--{$cover_size} chapter-section__cover--{$cover_type}"); ?>"
				src="<?php echo esc_url($cover_url); ?>"
				alt="<?php echo esc_attr($cover_alt); ?>" />
		<?php endif; ?>
	</div>
</section>