<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class TestimonialWidget extends WidgetBase {
	public function get_type(): string { return 'testimonial'; }
	public function get_title(): string { return 'Depoimento'; }
	public function get_icon(): string { return 'testimonial'; }
	public function get_defaults(): array {
		return array(
			'content' => 'Excelente escola e equipe dedicada.',
			'name'    => 'Maria Silva',
			'role'    => 'Responsável',
			'image'   => '',
			'align'   => 'left',
		);
	}
	public function sanitize_settings( array $settings ): array {
		$align = in_array( ( $settings['align'] ?? '' ), array( 'left', 'center', 'right' ), true ) ? (string) $settings['align'] : 'left';
		return array(
			'content' => sanitize_textarea_field( (string) ( $settings['content'] ?? '' ) ),
			'name'    => sanitize_text_field( (string) ( $settings['name'] ?? '' ) ),
			'role'    => sanitize_text_field( (string) ( $settings['role'] ?? '' ) ),
			'image'   => esc_url_raw( (string) ( $settings['image'] ?? '' ) ),
			'align'   => $align,
		);
	}
	public function render( Node $node, array $context = array() ): string {
		$s     = $node->settings;
		$align = esc_attr( (string) ( $s['align'] ?? 'left' ) );
		$img   = (string) ( $s['image'] ?? '' );
		$avatar = $img !== ''
			? '<img class="ab-testimonial__avatar" src="' . esc_url( $img ) . '" alt=""/>'
			: '<span class="ab-testimonial__avatar ab-testimonial__avatar--ph">👤</span>';
		return '<blockquote class="ab-testimonial" style="text-align:' . $align . '">'
			. '<p class="ab-testimonial__content">' . esc_html( (string) ( $s['content'] ?? '' ) ) . '</p>'
			. '<footer class="ab-testimonial__meta">' . $avatar
			. '<div><strong class="ab-testimonial__name">' . esc_html( (string) ( $s['name'] ?? '' ) ) . '</strong>'
			. '<span class="ab-testimonial__role">' . esc_html( (string) ( $s['role'] ?? '' ) ) . '</span></div>'
			. '</footer></blockquote>';
	}
}
