<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class SectionWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Seção';
	}

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

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

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

	public function get_defaults(): array {
		return array(
			'layout'    => 'full',
			'direction' => 'grid', // column | row | grid
			'columns'   => 1,     // equal column tracks (Elementor-like)
			'gap'       => '16px',
		);
	}

	public function get_controls(): array {
		return array(
			array(
				'type'    => 'select',
				'name'    => 'layout',
				'label'   => 'Largura',
				'options' => array(
					array( 'value' => 'boxed', 'label' => 'Caixa' ),
					array( 'value' => 'full', 'label' => 'Largura total' ),
				),
			),
			array(
				'type'    => 'select',
				'name'    => 'direction',
				'label'   => 'Layout interno',
				'options' => array(
					array( 'value' => 'column', 'label' => 'Empilhado' ),
					array( 'value' => 'row', 'label' => 'Flex linha' ),
					array( 'value' => 'grid', 'label' => 'Grade (colunas)' ),
				),
			),
			array(
				'type'  => 'number',
				'name'  => 'columns',
				'label' => 'Número de colunas',
			),
		);
	}

	public function sanitize_settings( array $settings ): array {
		$layout = isset( $settings['layout'] ) ? sanitize_key( (string) $settings['layout'] ) : 'full';
		if ( ! in_array( $layout, array( 'boxed', 'full' ), true ) ) {
			$layout = 'full';
		}
		$dir = isset( $settings['direction'] ) ? sanitize_key( (string) $settings['direction'] ) : 'grid';
		if ( ! in_array( $dir, array( 'column', 'row', 'grid' ), true ) ) {
			$dir = 'grid';
		}
		$cols = isset( $settings['columns'] ) ? absint( $settings['columns'] ) : 1;
		$cols = max( 1, min( 12, $cols ) );
		$gap  = isset( $settings['gap'] ) ? sanitize_text_field( (string) $settings['gap'] ) : '16px';

		$out = array(
			'layout'    => $layout,
			'direction' => $dir,
			'columns'   => $cols,
			'gap'       => $gap,
		);

		// Optional flex align (row sections / header-like layouts).
		$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' );
		if ( isset( $settings['justify_content'] ) ) {
			$just = sanitize_text_field( (string) $settings['justify_content'] );
			if ( in_array( $just, $allowed_just, true ) ) {
				$out['justify_content'] = $just;
			}
		}
		if ( isset( $settings['align_items'] ) ) {
			$align = sanitize_text_field( (string) $settings['align_items'] );
			if ( in_array( $align, $allowed_align, true ) ) {
				$out['align_items'] = $align;
			}
		}
		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 '';
	}
}
