<?php
/** Strict schemas for the curated native Astra bridge. */

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

/** @param array<string,array> $properties */
function wpagent_astra_object_schema( array $properties, array $required = array() ): array {
	$schema = array(
		'type'                 => 'object',
		'properties'           => $properties,
		'minProperties'        => 1,
		'additionalProperties' => false,
	);
	if ( $required ) {
		$schema['required'] = $required;
	}
	return $schema;
}

function wpagent_astra_palette_schema(): array {
	$color = array( 'type' => 'string', 'pattern' => '^#[A-Fa-f0-9]{6}$' );
	return wpagent_astra_object_schema(
		array(
			'colors' => wpagent_astra_object_schema(
				array(
					'primary'              => $color,
					'primary_hover'        => $color,
					'heading'              => $color,
					'text'                 => $color,
					'background'           => $color,
					'secondary_background' => $color,
					'alternate_background' => $color,
					'subtle_background'    => $color,
					'supporting'           => $color,
				)
			),
		),
		array( 'colors' )
	);
}

function wpagent_astra_blog_archive_schema(): array {
	return wpagent_astra_object_schema(
		array(
			'blog_layout'    => array( 'type' => 'string', 'enum' => array( 'blog-layout-classic', 'blog-layout-4', 'blog-layout-5', 'blog-layout-6' ) ),
			'posts_per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 500 ),
			'sidebar_layout' => array( 'type' => 'string', 'enum' => array( 'no-sidebar', 'left-sidebar', 'right-sidebar' ) ),
			'sidebar_style'  => array( 'type' => 'string', 'enum' => array( 'unboxed', 'boxed' ) ),
			'sidebar_width'  => array( 'type' => 'integer', 'minimum' => 15, 'maximum' => 50 ),
		)
	);
}

function wpagent_astra_single_content_schema( bool $with_related_posts ): array {
	$properties = array(
		'container_layout'  => array( 'type' => 'string', 'enum' => array( 'default', 'normal-width-container', 'narrow-width-container', 'full-width-container' ) ),
		'container_style'   => array( 'type' => 'string', 'enum' => array( 'boxed', 'unboxed' ) ),
		'sidebar_layout'    => array( 'type' => 'string', 'enum' => array( 'default', 'no-sidebar', 'left-sidebar', 'right-sidebar' ) ),
		'sidebar_style'     => array( 'type' => 'string', 'enum' => array( 'default', 'unboxed', 'boxed' ) ),
		'content_width'     => array( 'type' => 'string', 'enum' => array( 'default', 'custom' ) ),
		'content_max_width' => array( 'type' => 'integer', 'minimum' => 320, 'maximum' => 1920 ),
	);
	if ( $with_related_posts ) {
		$properties['related_posts_enabled'] = array( 'type' => 'boolean' );
		$properties['related_posts_count']   = array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 20 );
	}
	return wpagent_astra_object_schema( $properties );
}

function wpagent_astra_sidebar_schema(): array {
	return wpagent_astra_object_schema(
		array(
			'layout'         => array( 'type' => 'string', 'enum' => array( 'no-sidebar', 'left-sidebar', 'right-sidebar' ) ),
			'style'          => array( 'type' => 'string', 'enum' => array( 'unboxed', 'boxed' ) ),
			'width'          => array( 'type' => 'integer', 'minimum' => 15, 'maximum' => 50 ),
			'sticky_enabled' => array( 'type' => 'boolean' ),
		)
	);
}

function wpagent_astra_scroll_schema(): array {
	$color = array( 'type' => 'string', 'pattern' => '^(?:|#[A-Fa-f0-9]{6})$' );
	return wpagent_astra_object_schema(
		array(
			'enabled'    => array( 'type' => 'boolean' ),
			'position'   => array( 'type' => 'string', 'enum' => array( 'right', 'left' ) ),
			'on_devices' => array( 'type' => 'string', 'enum' => array( 'both', 'desktop', 'mobile' ) ),
			'icon_size'  => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50 ),
			'colors'     => wpagent_astra_object_schema(
				array(
					'icon_color'      => $color,
					'icon_bg_color'   => $color,
					'icon_h_color'    => $color,
					'icon_h_bg_color' => $color,
				)
			),
		)
	);
}

/** @param list<string> $components */
function wpagent_astra_component_list_schema( array $components ): array {
	return array(
		'type'        => 'array',
		'items'       => array( 'type' => 'string', 'enum' => $components ),
		'maxItems'    => count( $components ),
		'uniqueItems' => true,
	);
}

/** @param list<string> $zones @param list<string> $components */
function wpagent_astra_zone_schema( array $zones, array $components ): array {
	$properties = array();
	foreach ( $zones as $zone ) {
		$properties[ $zone ] = wpagent_astra_component_list_schema( $components );
	}
	return wpagent_astra_object_schema( $properties );
}

/** @param list<string> $components */
function wpagent_astra_header_device_schema( bool $mobile, array $components ): array {
	$positions = $mobile ? array( 'left', 'center', 'right' ) : array( 'left', 'left_center', 'center', 'right_center', 'right' );
	$sections  = array(
		'popup' => wpagent_astra_zone_schema( array( 'popup_content' ), $components ),
	);
	foreach ( array( 'above', 'primary', 'below' ) as $section ) {
		$zones = array_map( static fn( string $position ): string => $section . '_' . $position, $positions );
		$sections[ $section ] = wpagent_astra_zone_schema( $zones, $components );
	}
	return wpagent_astra_object_schema( $sections );
}

function wpagent_astra_header_layout_schema(): array {
	$components = array(
		'logo', 'button-1', 'button-2', 'button-3', 'html-1', 'html-2', 'html-3',
		'menu-1', 'menu-2', 'mobile-menu', 'mobile-trigger', 'widget-1', 'widget-2',
		'widget-3', 'social-icons-1', 'social-icons-2', 'search', 'woo-cart', 'edd-cart',
		'account', 'language-switcher',
	);
	return wpagent_astra_object_schema(
		array(
			'desktop' => wpagent_astra_header_device_schema( false, $components ),
			'mobile'  => wpagent_astra_header_device_schema( true, $components ),
		)
	);
}

/** @param list<string> $components */
function wpagent_astra_footer_device_schema( bool $mobile, array $components ): array {
	$sections = array();
	$columns  = $mobile ? 2 : 5;
	foreach ( array( 'above', 'primary', 'below' ) as $section ) {
		$zones = array();
		for ( $column = 1; $column <= $columns; $column++ ) {
			$zones[] = $section . '_' . $column;
		}
		$sections[ $section ] = wpagent_astra_zone_schema( $zones, $components );
	}
	return wpagent_astra_object_schema( $sections );
}

function wpagent_astra_footer_layout_schema(): array {
	$components = array(
		'copyright', 'menu', 'widget-1', 'widget-2', 'widget-3', 'widget-4',
		'social-icons-1', 'social-icons-2', 'html-1', 'html-2', 'html-3', 'button-1', 'button-2',
	);
	$properties = array(
		'desktop' => wpagent_astra_footer_device_schema( false, $components ),
		'mobile'  => wpagent_astra_footer_device_schema( true, $components ),
	);
	foreach ( array( 'above', 'primary', 'below' ) as $section ) {
		$properties[ $section . '_footer_columns' ] = array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 5 );
	}
	return wpagent_astra_object_schema( $properties );
}

/** @return array<string,array{label:string,read:string,write:string,input_schema:array}> */
function wpagent_astra_extended_operation_catalog(): array {
	$catalog = array(
		'global-palette'  => array(
			'label' => __( 'Globale Farbpalette', 'wp-agent-companion' ), 'read' => 'astra/get-global-palette',
			'write' => 'astra/update-global-palette', 'input_schema' => wpagent_astra_palette_schema(),
		),
		'paragraph-margin' => array(
			'label' => __( 'Absatzabstand', 'wp-agent-companion' ), 'read' => 'astra/get-paragraph-margin',
			'write' => 'astra/update-paragraph-margin', 'input_schema' => wpagent_astra_object_schema( array( 'margin_bottom' => array( 'type' => 'number', 'minimum' => 0.5, 'maximum' => 5 ) ), array( 'margin_bottom' ) ),
		),
		'link-underline'   => array(
			'label' => __( 'Link-Unterstreichungen', 'wp-agent-companion' ), 'read' => 'astra/get-link-underline',
			'write' => 'astra/update-link-underline', 'input_schema' => wpagent_astra_object_schema( array( 'enabled' => array( 'type' => 'boolean' ) ), array( 'enabled' ) ),
		),
		'blog-archive'     => array(
			'label' => __( 'Blogarchiv', 'wp-agent-companion' ), 'read' => 'astra/get-blog-archive',
			'write' => 'astra/update-blog-archive', 'input_schema' => wpagent_astra_blog_archive_schema(),
		),
		'single-post'      => array(
			'label' => __( 'Einzelbeiträge', 'wp-agent-companion' ), 'read' => 'astra/get-single-post',
			'write' => 'astra/update-single-post', 'input_schema' => wpagent_astra_single_content_schema( true ),
		),
		'single-page'      => array(
			'label' => __( 'Einzelseiten', 'wp-agent-companion' ), 'read' => 'astra/get-single-page',
			'write' => 'astra/update-single-page', 'input_schema' => wpagent_astra_single_content_schema( false ),
		),
		'sidebar'          => array(
			'label' => __( 'Seitenleiste', 'wp-agent-companion' ), 'read' => 'astra/get-sidebar',
			'write' => 'astra/update-sidebar', 'input_schema' => wpagent_astra_sidebar_schema(),
		),
		'scroll-to-top'    => array(
			'label' => __( 'Nach-oben-Schaltfläche', 'wp-agent-companion' ), 'read' => 'astra/get-scroll-to-top',
			'write' => 'astra/update-scroll-to-top', 'input_schema' => wpagent_astra_scroll_schema(),
		),
		'header-layout'    => array(
			'label' => __( 'Header-Builder-Layout', 'wp-agent-companion' ), 'read' => 'astra/get-header-builder',
			'write' => 'astra/update-header-builder', 'input_schema' => wpagent_astra_header_layout_schema(),
		),
		'footer-layout'    => array(
			'label' => __( 'Footer-Builder-Layout', 'wp-agent-companion' ), 'read' => 'astra/get-footer-builder',
			'write' => 'astra/update-footer-builder', 'input_schema' => wpagent_astra_footer_layout_schema(),
		),
	);
	for ( $level = 1; $level <= 6; $level++ ) {
		$catalog[ 'h' . $level . '-font' ] = array(
			/* translators: %d: heading level from 1 to 6. */
			'label'        => sprintf( __( 'H%d-Typografie', 'wp-agent-companion' ), $level ),
			'read'         => 'astra/get-font-h' . $level,
			'write'        => 'astra/update-font-h' . $level,
			'input_schema' => wpagent_astra_font_schema( true ),
		);
	}
	return $catalog;
}
