<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

use AuraBuilder\Document\Node;
use AuraBuilder\Widgets\WidgetBase;

final class ContainerWidget extends WidgetBase {

	public function get_type(): string {
		return 'container';
	}

	public function get_title(): string {
		return 'Container';
	}

	public function get_category(): string {
		return 'layout';
	}

	public function get_icon(): string {
		return 'box';
	}

	public function is_container(): bool {
		return true;
	}

	public function get_defaults(): array {
		return array(
			'display'         => 'flex',
			'direction'       => 'column',
			'gap'             => '16px',
			'width'           => '100%',
			'col_span'        => 12, // span in parent 12-col grid
			'justify_content' => 'flex-start',
			'align_items'     => 'stretch',
		);
	}

	public function get_controls(): array {
		return array(
			array(
				'type'    => 'select',
				'name'    => 'direction',
				'label'   => 'Direção',
				'options' => array(
					array( 'value' => 'column', 'label' => 'Coluna' ),
					array( 'value' => 'row', 'label' => 'Linha' ),
				),
			),
			array(
				'type'  => 'text',
				'name'  => 'gap',
				'label' => 'Gap',
			),
			array(
				'type'  => 'number',
				'name'  => 'col_span',
				'label' => 'Span (1–12)',
			),
			array(
				'type'    => 'select',
				'name'    => 'justify_content',
				'label'   => 'Alinhamento principal',
				'options' => array(
					array( 'value' => 'flex-start', 'label' => 'Início' ),
					array( 'value' => 'center', 'label' => 'Centro' ),
					array( 'value' => 'flex-end', 'label' => 'Fim' ),
					array( 'value' => 'space-between', 'label' => 'Espaçar' ),
					array( 'value' => 'space-around', 'label' => 'Espaçar em volta' ),
					array( 'value' => 'space-evenly', 'label' => 'Espaçar igual' ),
				),
			),
			array(
				'type'    => 'select',
				'name'    => 'align_items',
				'label'   => 'Alinhamento cruzado',
				'options' => array(
					array( 'value' => 'flex-start', 'label' => 'Início' ),
					array( 'value' => 'center', 'label' => 'Centro' ),
					array( 'value' => 'flex-end', 'label' => 'Fim' ),
					array( 'value' => 'stretch', 'label' => 'Esticar' ),
					array( 'value' => 'baseline', 'label' => 'Baseline' ),
				),
			),
		);
	}

	public function sanitize_settings( array $settings ): array {
		$dir = isset( $settings['direction'] ) ? sanitize_key( (string) $settings['direction'] ) : 'column';
		if ( ! in_array( $dir, array( 'column', 'row' ), true ) ) {
			$dir = 'column';
		}
		$gap = isset( $settings['gap'] ) ? sanitize_text_field( (string) $settings['gap'] ) : '16px';
		$w   = isset( $settings['width'] ) ? sanitize_text_field( (string) $settings['width'] ) : '100%';
		if ( $w === '' ) {
			$w = '100%';
		}
		$span = isset( $settings['col_span'] ) ? absint( $settings['col_span'] ) : 12;
		$span = max( 1, min( 12, $span ) );

		$allowed_just  = array( 'flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly', 'start', 'end' );
		$allowed_align = array( 'flex-start', 'center', 'flex-end', 'stretch', 'baseline', 'start', 'end' );
		$just = isset( $settings['justify_content'] ) ? sanitize_text_field( (string) $settings['justify_content'] ) : 'flex-start';
		if ( ! in_array( $just, $allowed_just, true ) ) {
			$just = 'flex-start';
		}
		$default_align = $dir === 'row' ? 'flex-start' : 'stretch';
		$align = isset( $settings['align_items'] ) ? sanitize_text_field( (string) $settings['align_items'] ) : $default_align;
		if ( ! in_array( $align, $allowed_align, true ) ) {
			$align = $default_align;
		}

		$out = array(
			'display'         => 'flex',
			'direction'       => $dir,
			'gap'             => $gap,
			'width'           => $w,
			'col_span'        => $span,
			'justify_content' => $just,
			'align_items'     => $align,
		);
		// Keep editor label (was dropped before — broke layer names).
		if ( isset( $settings['display_name'] ) && is_string( $settings['display_name'] ) ) {
			$name = sanitize_text_field( $settings['display_name'] );
			if ( $name !== '' ) {
				$out['display_name'] = $name;
			}
		}
		return $out;
	}

	public function render( Node $node, array $context = array() ): string {
		return '';
	}
}
