<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Repeater;
use Elementor\Plugin;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Floating_Count_Controls;

class Sifency_Addons_Reveal_Showcase extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Floating_Count_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-reveal-showcase',
			'title'          => 'Sifency Reveal Showcase',
			'icon'           => 'eicon-scroll',
			'category'       => array( 'sifency-addons-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-frontend-script', 'sifency-reveal-showcase-script' ),
			'style_depends'  => array( 'sifency-frontend-style', 'sifency-reveal-showcase-style' ),
		);
	}

	// =========================================================================
	// CONTROLS
	// =========================================================================

	protected function register_controls() {

		// ── Showcase Settings ─────────────────────────────────────────────────
		$this->start_controls_section(
			'section_layout_config',
			array(
				'label' => __( 'Showcase Settings', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'data_source',
			array(
				'label'   => __( 'Content Source', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'manual'  => __( 'Manual Selection', 'sifency-addons' ),
					'dynamic' => __( 'Dynamic (Posts/CPT)', 'sifency-addons' ),
				),
				'default' => 'manual',
			)
		);

		$this->add_control(
			'layout_style',
			array(
				'label'   => __( 'Select Layout', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'reveal' => __( 'Stacked Reveal', 'sifency-addons' ),
					'grid'   => __( 'Scattered Grid (Masonry)', 'sifency-addons' ),
					'clip'   => __( 'Clip Showcase', 'sifency-addons' ),
					'sticky' => __( 'Sticky Media Reveal', 'sifency-addons' ),
				),
				'default' => 'reveal',
			)
		);

		$this->add_control(
			'content_layout_type',
			array(
				'label'   => __( 'Content Display', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'overlay' => __( 'Overlay (Text on Image)', 'sifency-addons' ),
					'stacked' => __( 'Stacked (Text below Image)', 'sifency-addons' ),
				),
				'default' => 'overlay',
			)
		);

		$this->add_control(
			'animation_style',
			array(
				'label'     => __( 'Animation Style', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'clip-reveal' => __( 'Clip Reveal', 'sifency-addons' ),
					'slide-up'    => __( 'Slide Up', 'sifency-addons' ),
					'fade-scale'  => __( 'Fade + Scale', 'sifency-addons' ),
					'swipe-in'    => __( 'Swipe In', 'sifency-addons' ),
				),
				'default'   => 'clip-reveal',
				'condition' => array( 'layout_style' => 'reveal' ),
			)
		);

		$this->add_control(
			'scroll_speed',
			array(
				'label'     => __( 'Scroll Speed (%)', 'sifency-addons' ),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 100,
				'min'       => 50,
				'max'       => 500,
				'step'      => 10,
				'condition' => array( 'layout_style' => array( 'reveal', 'sticky' ) ),
			)
		);

		$this->add_control(
			'magnetic_snap',
			array(
				'label'        => esc_html__( 'Magnetic Snap', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => 'yes',
				'label_on'     => esc_html__( 'On', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Off', 'sifency-addons' ),
				'return_value' => 'yes',
				'condition'    => array( 'layout_style' => 'reveal' ),
				'description'  => esc_html__( 'Smoothly snap to each card during scroll.', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'show_title',
			array(
				'label' => esc_html__( 'Show Title', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);

		$this->add_control(
			'show_subtitle',
			array(
				'label' => esc_html__( 'Show Subtitle', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);

		$this->add_control(
			'show_category',
			array(
				'label' => esc_html__( 'Show Category/Tag', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);

		$this->add_control(
			'show_excerpt',
			array(
				'label' => esc_html__( 'Show Excerpt', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);

		$this->add_control(
			'show_read_more',
			array(
				'label' => esc_html__( 'Show Button', 'sifency-addons' ),
				'type'  => Controls_Manager::SWITCHER,
			)
		);

		$this->add_control(
			'show_counter',
			array(
				'label'   => esc_html__( 'Show Counter', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'no',
			)
		);

		$this->add_control(
			'counter_position',
			array(
				'label'     => __( 'Counter Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'top-left'      => __( 'Top Left', 'sifency-addons' ),
					'top-center'    => __( 'Top Center', 'sifency-addons' ),
					'top-right'     => __( 'Top Right', 'sifency-addons' ),
					'center-left'   => __( 'Center Left', 'sifency-addons' ),
					'center'        => __( 'Center', 'sifency-addons' ),
					'center-right'  => __( 'Center Right', 'sifency-addons' ),
					'bottom-left'   => __( 'Bottom Left', 'sifency-addons' ),
					'bottom-center' => __( 'Bottom Center', 'sifency-addons' ),
					'bottom-right'  => __( 'Bottom Right', 'sifency-addons' ),
				),
				'default'   => 'top-right',
				'condition' => array( 'show_counter' => 'yes' ),
			)
		);

		$this->add_sifency_addons_floating_count_controls();

		$this->add_control(
			'read_more_text',
			array(
				'label'     => esc_html__( 'Button Text', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => esc_html__( 'View Project', 'sifency-addons' ),
				'condition' => array( 'show_read_more' => 'yes' ),
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'bg_text_',
				'label'    => esc_html__( 'Background Text Alignment', 'sifency-addons' ),
				'selector' => '.sifency-addons-reveal-bg-layer',
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'card_',
				'label'    => esc_html__( 'Card', 'sifency-addons' ),
				'selector' => '.sifency-reveal-container[data-layout="grid"] .sifency-addons-reveal-cards-layer',
			)
		);

		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'content_',
				'label'    => esc_html__( 'Card Content Alignment', 'sifency-addons' ),
				'selector' => '.sa-reveal-card-rich-content .rich-inner-wrapper',
			)
		);

		$this->end_controls_section();

		// ── Background Text ───────────────────────────────────────────────────
		$this->start_controls_section(
			'section_bg',
			array(
				'label' => __( 'Background Text', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'bg_text',
			array(
				'label'   => __( 'Title Text', 'sifency-addons' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => '<h1>Crafting Digital Experiences</h1>',
			)
		);

		$this->add_control(
			'bg_template',
			array(
				'type'    => Controls_Manager::SELECT2,
				'options' => function_exists( 'sifency_addons_get_elementor_templates' )
					? sifency_addons_get_elementor_templates()
					: array(),
			)
		);

		$this->add_control(
			'bg_scale_effect',
			array(
				'label'   => __( 'Parallax Scale', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->end_controls_section();

		// ── Manual Cards ──────────────────────────────────────────────────────
		$this->start_controls_section(
			'section_cards',
			array(
				'label'     => __( 'Manual Cards', 'sifency-addons' ),
				'condition' => array( 'data_source' => 'manual' ),
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'content_type',
			array(
				'label'   => __( 'Content Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => array(
					'image'    => __( 'Image & Tag', 'sifency-addons' ),
					'content'  => __( 'Rich Text / Custom', 'sifency-addons' ),
					'template' => __( 'Elementor Template', 'sifency-addons' ),
				),
				'default' => 'content',
			)
		);

		$repeater->add_control(
			'image',
			array(
				'label'   => __( 'Card Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array( 'url' => Utils::get_placeholder_image_src() ),
			)
		);

		$repeater->add_control(
			'tag',
			array(
				'label'   => __( 'Tag Label', 'sifency-addons' ),
				'type'    => Controls_Manager::TEXT,
				'default' => 'DESIGN',
			)
		);

		$repeater->add_control(
			'subtitle',
			array(
				'label'     => __( 'Subtitle', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Project 01',
				'condition' => array( 'content_type' => 'content' ),
			)
		);

		$repeater->add_control(
			'title',
			array(
				'label'     => __( 'Title', 'sifency-addons' ),
				'type'      => Controls_Manager::TEXT,
				'default'   => 'Creative Reveal',
				'condition' => array( 'content_type' => 'content' ),
			)
		);

		$repeater->add_control(
			'description',
			array(
				'label'     => __( 'Description', 'sifency-addons' ),
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => 'High-end animations for modern websites.',
				'condition' => array( 'content_type' => 'content' ),
			)
		);

		$repeater->add_control(
			'link',
			array(
				'label' => __( 'Link', 'sifency-addons' ),
				'type'  => Controls_Manager::URL,
			)
		);

		$repeater->add_control(
			'template_id',
			array(
				'label'     => __( 'Select Template', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT2,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' )
					? sifency_addons_get_elementor_templates()
					: array(),
				'condition' => array( 'content_type' => 'template' ),
			)
		);

		$this->add_control(
			'cards_list',
			array(
				'label'       => __( 'Cards', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '{{{ tag || title || "Card" }}}',
			)
		);

		$this->end_controls_section();

		// ── Dynamic Query ─────────────────────────────────────────────────────
		$this->add_sifency_arg_controls(
			array(
				'condition'    => array( 'data_source' => 'dynamic' ),
				'default_post' => 'portfolio',
			)
		);

		$this->add_sifency_addons_help_controls();

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'card_',
				'label'    => esc_html__( 'Card Inner', 'sifency-addons' ),
				'selector' => '.sa-revel-card-inner',
			)
		);

		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'con_',
				'label'    => esc_html__( 'Content Wrapper', 'sifency-addons' ),
				'selector' => '.rich-inner-wrapper',
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'main_title_',
				'label'           => 'Main Title',
				'selector'        => '.sifency-addons-reveal-bg-text h2',
				'color_only'      => true,
				'show_blend_mode' => true,
				'exclude'         => array( 'tabs', 'transition', 'border', 'shadow', 'stroke', 'radius', 'bg' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'title_',
				'label'           => 'Heading',
				'selector'        => '{{WRAPPER}} .sa-reveal-card-rich-title',
				'exclude'         => array( 'transition', 'border', 'radius', 'bg' ),
				'show_max_width'  => true,
				'condition'       => array( 'show_title' => 'yes' ),
				'show_blend_mode' => true,
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'excerpt_',
				'label'           => 'Excerpt',
				'selector'        => '{{WRAPPER}} .card-rich-desc',
				'color_only'      => true,
				'show_max_width'  => true,
				'exclude'         => array( 'tabs', 'transition', 'border', 'shadow', 'stroke', 'radius', 'bg' ),
				'condition'       => array( 'show_excerpt' => 'yes' ),
				'show_blend_mode' => true,
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'subtitle_cat_',
				'label'           => 'Category/Sub',
				'selector'        => '{{WRAPPER}} .card-rich-subtitle, {{WRAPPER}} .sa-revel-card-tag-simple',
				'color_only'      => true,
				'exclude'         => array( 'tabs', 'transition', 'border', 'shadow', 'stroke', 'radius', 'bg' ),
				'condition'       => array( 'show_category' => 'yes' ),
				'show_blend_mode' => true,
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'          => 'coun_',
				'label'           => 'Counter',
				'selector'        => '.sgc-current',
				'color_only'      => true,
				'exclude'         => array( 'tabs', 'transition', 'border', 'shadow', 'stroke', 'radius', 'bg' ),
				'condition'       => array( 'show_counter' => 'yes' ),
				'show_blend_mode' => true,
			)
		);

		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector'      => '.card-rich-btn',
				'section_label' => 'Button Style',
				'condition'     => array( 'show_read_more' => 'yes' ),
			)
		);

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'counter_',
				'label'      => 'Counter',
				'selector'   => '{{WRAPPER}} .sifency-global-counter',
				'color_only' => true,
				'condition'  => array( 'show_counter' => 'yes' ),
				'exclude'    => array( 'tabs', 'transition', 'border', 'shadow', 'stroke', 'radius', 'bg' ),
			)
		);

		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_sifency_addons_floating_count_style_controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}

	// =========================================================================
	// PRIVATE HELPERS
	// =========================================================================
	/**
	 * Ensure elementor-frontend style handle is registered before rendering
	 * embedded template content. WP 6.9.1 validates dependencies at
	 * WP_Styles::add() time, so the handle must exist before the template's
	 * own stylesheet is registered.
	 */
	private function ensure_elementor_frontend_style_registered(): void {
		if ( ! wp_style_is( 'elementor-frontend', 'registered' ) ) {
			wp_register_style(
				'elementor-frontend',
				ELEMENTOR_ASSETS_URL . 'css/frontend.min.css',
				array(),
				ELEMENTOR_VERSION
			);
		}
	}
	/**
	 * Resolves an Elementor media-field array to a usable URL.
	 *
	 * Priority:
	 *   1. wp_get_attachment_url( $id ) — always current-site correct; survives XML / OCDI import.
	 *   2. Stored URL                   — last resort; may be stale after import.
	 *
	 * @param  array $media_field  e.g. $item['image'] = ['id' => 12, 'url' => '...']
	 * @return string               Resolved URL or empty string.
	 */
	private function resolve_media_url( array $media_field ): string {
		$id  = ! empty( $media_field['id'] ) ? (int) $media_field['id'] : 0;
		$url = ! empty( $media_field['url'] ) ? $media_field['url'] : '';

		if ( $id > 0 ) {
			$resolved = wp_get_attachment_url( $id );
			if ( ! empty( $resolved ) ) {
				return $resolved;
			}
		}

		return $url;
	}

	// =========================================================================
	// RENDER
	// =========================================================================

	protected function render_dom( array $context ): void {
		$this->sifency_addons_render_custom_css();
		$settings = $this->get_settings_for_display();
		$layout   = $settings['layout_style'] ?? 'reveal';
		?>
		<div class="sifency-reveal-container"
			 data-layout="<?php echo esc_attr( $layout ); ?>"
			 data-animation="<?php echo esc_attr( $settings['animation_style'] ?? 'clip-reveal' ); ?>"
			 data-speed="<?php echo esc_attr( $settings['scroll_speed'] ?? 100 ); ?>"
			 data-parallax="<?php echo esc_attr( $settings['bg_scale_effect'] ?? 'yes' ); ?>"
			 data-snap="<?php echo esc_attr( $settings['magnetic_snap'] ?? 'yes' ); ?>"
		>
			<?php if ( 'yes' === ( $settings['show_counter'] ?? 'no' ) ) : ?>
				<div class="sifency-global-counter sgc-pos-<?php echo esc_attr( $settings['counter_position'] ?? 'top-right' ); ?>">
					<span class="sgc-current">01</span>
				</div>
			<?php endif; ?>

			<?php $this->render_sifency_floating_count( $settings, 'sgc-current' ); ?>

			<div class="sifency-addons-reveal-bg-layer">
				<div class="sifency-addons-reveal-bg-text">
					<?php
					if ( ! empty( $settings['bg_template'] ) && get_post_status( $settings['bg_template'] ) ) {
						$this->ensure_elementor_frontend_style_registered();
						// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder output
						echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $settings['bg_template'] );
					} else {
						echo wp_kses_post( $settings['bg_text'] );
					}
					?>
				</div>
			</div>

			<div class="sifency-addons-reveal-cards-layer">
				<?php
				if ( 'dynamic' === $settings['data_source'] ) {
					$query_args = $this->get_sifency_addons_query_args();
					$query      = new \WP_Query( $query_args );

					if ( $query->have_posts() ) {
						while ( $query->have_posts() ) {
							$query->the_post();
							$index = $query->current_post;

							$excerpt = has_excerpt()
								? get_the_excerpt()
								: wp_trim_words( strip_shortcodes( get_the_content() ), 20 );

							// Dynamic source: use thumbnail ID → wp_get_attachment_url()
							// get_the_post_thumbnail_url() is already site-local, so it's safe.
							$thumbnail_id = get_post_thumbnail_id();
							$thumb_url    = $thumbnail_id
								? wp_get_attachment_url( $thumbnail_id )
								: '';

							$post_item = array(
								'_id'          => get_the_ID(),
								'content_type' => 'content',
								'image'        => array(
									'id'  => $thumbnail_id,
									'url' => $thumb_url,
								),
								'title'        => get_the_title(),
								'subtitle'     => get_the_date(),
								'description'  => $excerpt,
								'tag'          => '',
								'link'         => array( 'url' => get_permalink() ),
							);

							$terms = get_the_terms( get_the_ID(), 'category' );
							if ( $terms && ! is_wp_error( $terms ) ) {
								$post_item['tag'] = $terms[0]->name;
							}

							$this->render_single_card( $post_item, $index, $settings );
						}
						wp_reset_postdata();
					}
				} else {
					foreach ( $settings['cards_list'] as $index => $item ) {
						$this->render_single_card( $item, $index, $settings );
					}
				}
				?>
			</div>
		</div>
		<?php
	}

	/**
	 * Renders a single card.
	 *
	 * Image URL is always resolved from the attachment ID via resolve_media_url()
	 * so the widget works correctly after XML / OCDI imports.
	 *
	 * @param array $item     Card data (repeater item or synthesised dynamic item).
	 * @param int   $index    Zero-based card index.
	 * @param array $settings Widget settings.
	 */
	private function render_single_card( array $item, int $index, array $settings ): void {
		$oi             = $index + 1;
		$parity_class   = ( $oi % 2 !== 0 ) ? 'odd-item' : 'even-item';
		$repeater_class = isset( $item['_id'] )
			? ' elementor-repeater-item-' . esc_attr( $item['_id'] )
			: '';

		$content_layout = $settings['content_layout_type'] ?? 'overlay';

		// ── Resolve image URL from attachment ID ──────────────────────────────
		// Using wp_get_attachment_url() ensures the URL is always current-site
		// correct and never stale after XML / OCDI imports.
		$image_url = $this->resolve_media_url( $item['image'] ?? array() );

		// Build inline background-image style for overlay layout.
		$bg_style = '';
		if ( 'overlay' === $content_layout && ! empty( $image_url ) ) {
			$bg_style = 'style="background-image: url(' . esc_url( $image_url ) . '); background-size: cover; background-position: center;"';
		}
		?>
		<div class="sifency-addons-reveal-card <?php echo esc_attr( $parity_class . $repeater_class ); ?>"
			 data-index="<?php echo absint( $index ); ?>">

			<div class="sa-revel-card-inner <?php echo esc_attr( 'layout-' . $content_layout ); ?>"
				 <?php
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- pre-escaped above
					echo wp_kses_post( $bg_style );
					?>
					>

				<?php if ( 'stacked' === $content_layout && ! empty( $image_url ) ) : ?>
					<div class="sa-reveal-card-image">
						<img src="<?php echo esc_url( $image_url ); ?>"
							 alt="<?php echo esc_attr( $item['title'] ?? '' ); ?>">
						<?php if ( ! empty( $item['link']['url'] ) ) : ?>
							<a href="<?php echo esc_url( $item['link']['url'] ); ?>" class="card-link-mask"></a>
						<?php endif; ?>
					</div>
				<?php endif; ?>

				<div class="sa-reveal-card-rich-content">
					<div class="rich-inner-wrapper">

						<?php if ( 'yes' === ( $settings['show_category'] ?? '' ) && ! empty( $item['tag'] ) ) : ?>
							<span class="sa-revel-card-tag-simple"><?php echo esc_html( $item['tag'] ); ?></span>
						<?php endif; ?>

						<?php if ( ! empty( $settings['show_subtitle'] ) && ! empty( $item['subtitle'] ) ) : ?>
							<h5 class="card-rich-subtitle"><?php echo esc_html( $item['subtitle'] ); ?></h5>
						<?php endif; ?>

						<?php if ( ! empty( $settings['show_title'] ) && ! empty( $item['title'] ) ) : ?>
							<h2 class="sa-reveal-card-rich-title">
								<?php if ( ! empty( $item['link']['url'] ) ) : ?>
									<a href="<?php echo esc_url( $item['link']['url'] ); ?>">
								<?php endif; ?>
								<?php echo esc_html( $item['title'] ); ?>
								<?php if ( ! empty( $item['link']['url'] ) ) : ?>
									</a>
								<?php endif; ?>
							</h2>
						<?php endif; ?>

						<?php if ( ! empty( $settings['show_excerpt'] ) && ! empty( $item['description'] ) ) : ?>
							<div class="card-rich-desc">
								<?php echo wp_kses_post( $item['description'] ); ?>
							</div>
						<?php endif; ?>

						<?php
						if ( 'yes' === ( $settings['show_read_more'] ?? '' ) ) :
							$btn_text = ! empty( $item['btn_text'] ) ? $item['btn_text'] : ( $settings['read_more_text'] ?? '' );
							$url      = ! empty( $item['link']['url'] ) ? $item['link']['url'] : '#';
							?>
							<a href="<?php echo esc_url( $url ); ?>" class="card-rich-btn">
								<?php echo esc_html( $btn_text ); ?>
							</a>
						<?php endif; ?>

					</div>
				</div>

				<?php if ( 'overlay' === $content_layout && ! empty( $item['link']['url'] ) ) : ?>
					<a href="<?php echo esc_url( $item['link']['url'] ); ?>" class="card-link-mask"></a>
				<?php endif; ?>

			</div>
		</div>
		<?php
	}
}
