<?php
/**
 * Preview Card — image card with title, text and CTA (hover effects).
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class PreviewCardWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Card de preview';
	}

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

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

	public function get_defaults(): array {
		return array(
			'image'       => '',
			'title'       => 'Feira das Nações',
			'subtitle'    => 'Evento escolar',
			'text'        => 'Uma imersão cultural com apresentações, gastronomia e oficinas para toda a família.',
			'button_text' => 'Saiba mais',
			'button_url'  => '#',
			'style'       => 'hover',
			'radius'      => '16',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$style = sanitize_key( (string) ( $settings['style'] ?? 'hover' ) );
		if ( ! in_array( $style, array( 'hover', 'below', 'overlay' ), true ) ) {
			$style = 'hover';
		}
		return array(
			'image'       => esc_url_raw( (string) ( $settings['image'] ?? '' ) ),
			'title'       => sanitize_text_field( (string) ( $settings['title'] ?? '' ) ),
			'subtitle'    => sanitize_text_field( (string) ( $settings['subtitle'] ?? '' ) ),
			'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,
			'radius'      => (string) max( 0, min( 40, (int) ( $settings['radius'] ?? 16 ) ) ),
		);
	}

	public function render( Node $node, array $context = array() ): string {
		$s      = $node->settings;
		$img    = (string) ( $s['image'] ?? '' );
		$title  = (string) ( $s['title'] ?? '' );
		$sub    = (string) ( $s['subtitle'] ?? '' );
		$text   = (string) ( $s['text'] ?? '' );
		$btn    = (string) ( $s['button_text'] ?? '' );
		$url    = (string) ( $s['button_url'] ?? '#' );
		$style  = esc_attr( (string) ( $s['style'] ?? 'hover' ) );
		$radius = (int) ( $s['radius'] ?? 16 );

		if ( $img === '' && $title === '' ) {
			return ! empty( $context['editor'] )
				? '<div class="ab-preview-card ab-empty">Card de preview — configure imagem e título</div>'
				: '';
		}

		$html  = '<article class="ab-preview-card ab-preview-card--' . $style . '" style="--ab-pc-r:' . esc_attr( (string) $radius ) . 'px">';
		$html .= '<div class="ab-preview-card__media">';
		if ( $img !== '' ) {
			$html .= '<img src="' . esc_url( $img ) . '" alt="' . esc_attr( $title ) . '" loading="lazy"/>';
		} else {
			$html .= '<div class="ab-preview-card__ph"></div>';
		}
		$html .= '<div class="ab-preview-card__shade"></div>';
		$html .= '</div>';
		$html .= '<div class="ab-preview-card__body">';
		if ( $sub !== '' ) {
			$html .= '<span class="ab-preview-card__sub">' . esc_html( $sub ) . '</span>';
		}
		if ( $title !== '' ) {
			$html .= '<h3 class="ab-preview-card__title">' . esc_html( $title ) . '</h3>';
		}
		if ( $text !== '' ) {
			$html .= '<p class="ab-preview-card__text">' . esc_html( $text ) . '</p>';
		}
		if ( $btn !== '' ) {
			$html .= '<a class="ab-preview-card__btn" href="' . esc_url( $url !== '' ? $url : '#' ) . '">' . esc_html( $btn ) . '</a>';
		}
		$html .= '</div></article>';
		return $html;
	}
}
