<?php
/**
 * Info Box — icon + title + text + optional CTA.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class InfoBoxWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Caixa de informação';
	}

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

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

	public function get_defaults(): array {
		return array(
			'icon'        => '📚',
			'title'       => 'Ensino de excelência',
			'text'        => 'Metodologia ativa, professores qualificados e acompanhamento individual do aluno.',
			'button_text' => 'Conhecer',
			'button_url'  => '#',
			'style'       => 'card',
			'accent'      => '#7c3aed',
			'align'       => 'center',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$style = sanitize_key( (string) ( $settings['style'] ?? 'card' ) );
		if ( ! in_array( $style, array( 'card', 'border', 'flat', 'soft' ), true ) ) {
			$style = 'card';
		}
		$align = in_array( ( $settings['align'] ?? '' ), array( 'left', 'center', 'right' ), true )
			? (string) $settings['align']
			: 'center';
		$accent = trim( (string) ( $settings['accent'] ?? '#7c3aed' ) );
		if ( ! preg_match( '/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $accent ) ) {
			$accent = '#7c3aed';
		}
		return array(
			'icon'        => sanitize_text_field( (string) ( $settings['icon'] ?? '' ) ),
			'title'       => sanitize_text_field( (string) ( $settings['title'] ?? '' ) ),
			'text'        => sanitize_textarea_field( (string) ( $settings['text'] ?? '' ) ),
			'button_text' => sanitize_text_field( (string) ( $settings['button_text'] ?? '' ) ),
			'button_url'  => esc_url_raw( (string) ( $settings['button_url'] ?? '' ) ),
			'style'       => $style,
			'accent'      => strtolower( $accent ),
			'align'       => $align,
		);
	}

	public function render( Node $node, array $context = array() ): string {
		$s      = $node->settings;
		$icon   = (string) ( $s['icon'] ?? '' );
		$title  = (string) ( $s['title'] ?? '' );
		$text   = (string) ( $s['text'] ?? '' );
		$btn    = (string) ( $s['button_text'] ?? '' );
		$url    = (string) ( $s['button_url'] ?? '#' );
		$style  = esc_attr( (string) ( $s['style'] ?? 'card' ) );
		$accent = esc_attr( (string) ( $s['accent'] ?? '#7c3aed' ) );
		$align  = esc_attr( (string) ( $s['align'] ?? 'center' ) );

		if ( $title === '' && $text === '' ) {
			return ! empty( $context['editor'] )
				? '<div class="ab-info-box ab-empty">Caixa de informação</div>'
				: '';
		}

		$html  = '<div class="ab-info-box ab-info-box--' . $style . ' ab-info-box--' . $align . '" style="--ab-ib-accent:' . $accent . '">';
		if ( $icon !== '' ) {
			$html .= '<div class="ab-info-box__icon">' . esc_html( $icon ) . '</div>';
		}
		if ( $title !== '' ) {
			$html .= '<h3 class="ab-info-box__title">' . esc_html( $title ) . '</h3>';
		}
		if ( $text !== '' ) {
			$html .= '<p class="ab-info-box__text">' . esc_html( $text ) . '</p>';
		}
		if ( $btn !== '' ) {
			$html .= '<a class="ab-info-box__btn" href="' . esc_url( $url !== '' ? $url : '#' ) . '">' . esc_html( $btn ) . '</a>';
		}
		$html .= '</div>';
		return $html;
	}
}
