<?php
/**
 * Shared base for Niwa-generated Elementor template widgets.
 *
 * @package Niwa_AI
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

abstract class Niwa_AI_Elementor_Template_Widget_Base extends \Elementor\Widget_Base {
	protected function niwa_snippet_id() {
		return 0;
	}

	protected function niwa_template_config() {
		return array();
	}

	public function get_icon() {
		return 'eicon-code';
	}

	public function get_categories() {
		return array( 'niwa-generated', 'general' );
	}

	public function get_style_depends() {
		$id = intval( $this->niwa_snippet_id() );
		return $id > 0 ? array( 'niwa-template-widget-style-' . $id ) : array();
	}

	public function get_script_depends() {
		$id = intval( $this->niwa_snippet_id() );
		return $id > 0 ? array( 'niwa-template-widget-script-' . $id ) : array();
	}

	protected function register_controls() {
		$config   = $this->config();
		$kind     = $this->kind();
		$defaults = $this->defaults();

		$this->start_controls_section(
			'niwa_content',
			array(
				'label' => __( 'Content', 'niwa-ai' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'eyebrow',
			array(
				'label'   => __( 'Eyebrow', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::TEXT,
				'default' => $this->default_text( 'eyebrow', __( 'Explore', 'niwa-ai' ) ),
			)
		);
		$this->add_control(
			'title',
			array(
				'label'   => __( 'Title', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::TEXT,
				'default' => $this->default_text( 'title', $this->get_title() ),
			)
		);
		$this->add_control(
			'intro',
			array(
				'label'   => __( 'Intro', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::TEXTAREA,
				'default' => $this->default_text( 'intro', '' ),
			)
		);
		$this->add_control(
			'cta_text',
			array(
				'label'   => __( 'CTA Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::TEXT,
				'default' => $this->default_text( 'cta_text', __( 'Learn more', 'niwa-ai' ) ),
			)
		);
		$this->add_control(
			'cta_url',
			array(
				'label'   => __( 'CTA URL', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::URL,
				'default' => array(
					'url' => $this->default_text( 'cta_url', '#' ),
				),
			)
		);

		if ( in_array( $kind, array( 'image_reveal_menu', 'team_accordion', 'product_finder_quiz', 'habit_tracker' ), true ) ) {
			$repeater = new \Elementor\Repeater();
			$repeater->add_control(
				'item_label',
				array(
					'label'   => __( 'Label', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => __( 'Item label', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'item_description',
				array(
					'label'   => __( 'Description', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXTAREA,
					'default' => __( 'Short supporting copy.', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'item_image',
				array(
					'label' => __( 'Image', 'niwa-ai' ),
					'type'  => \Elementor\Controls_Manager::MEDIA,
				)
			);
			$this->add_control(
				'items',
				array(
					'label'       => 'team_accordion' === $kind ? __( 'Cards', 'niwa-ai' ) : ( 'product_finder_quiz' === $kind ? __( 'Choices', 'niwa-ai' ) : ( 'habit_tracker' === $kind ? __( 'Habits', 'niwa-ai' ) : __( 'Menu Items', 'niwa-ai' ) ) ),
					'type'        => \Elementor\Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => $this->default_repeater_items( $defaults ),
					'title_field' => '{{{ item_label }}}',
				)
			);
		}

		if ( 'testimonial_carousel' === $kind ) {
			$repeater = new \Elementor\Repeater();
			$repeater->add_control(
				'testimonial_content',
				array(
					'label'   => __( 'Quote', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXTAREA,
					'default' => __( 'The result was clear and ready to publish.', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'testimonial_name',
				array(
					'label'   => __( 'Name', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => __( 'Customer', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'testimonial_role',
				array(
					'label'   => __( 'Role', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => __( 'Verified customer', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'testimonial_rating',
				array(
					'label'   => __( 'Rating', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::NUMBER,
					'min'     => 1,
					'max'     => 5,
					'step'    => 1,
					'default' => 5,
				)
			);
			$this->add_control(
				'testimonials',
				array(
					'label'       => __( 'Testimonials', 'niwa-ai' ),
					'type'        => \Elementor\Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => $this->default_testimonials( $defaults ),
					'title_field' => '{{{ testimonial_name }}}',
				)
			);
		}

		if ( 'pricing_cards' === $kind ) {
			$repeater = new \Elementor\Repeater();
			$repeater->add_control(
				'plan_title',
				array(
					'label'   => __( 'Plan', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => __( 'Growth', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'plan_price',
				array(
					'label'   => __( 'Price', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => '$149',
				)
			);
			$repeater->add_control(
				'plan_features',
				array(
					'label'   => __( 'Features', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXTAREA,
					'default' => "Custom layout\nAutomations\nPriority support",
				)
			);
			$repeater->add_control(
				'plan_button',
				array(
					'label'   => __( 'Button Text', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => __( 'Choose plan', 'niwa-ai' ),
				)
			);
			$repeater->add_control(
				'plan_highlighted',
				array(
					'label'        => __( 'Highlighted', 'niwa-ai' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'return_value' => 'yes',
					'default'      => '',
				)
			);
			$this->add_control(
				'plans',
				array(
					'label'       => __( 'Plans', 'niwa-ai' ),
					'type'        => \Elementor\Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'default'     => $this->default_plans( $defaults ),
					'title_field' => '{{{ plan_title }}}',
				)
			);
		}

		if ( 'quote_calculator' === $kind ) {
			$this->add_control(
				'quote_unit_price',
				array(
					'label'   => __( 'Unit Price', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::NUMBER,
					'min'     => 1,
					'step'    => 1,
					'default' => intval( $this->default_text( 'quote_unit_price', 80 ) ),
				)
			);
			$this->add_control(
				'quote_quantity_min',
				array(
					'label'   => __( 'Minimum Quantity', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::NUMBER,
					'min'     => 1,
					'step'    => 1,
					'default' => intval( $this->default_text( 'quote_quantity_min', 1 ) ),
				)
			);
			$this->add_control(
				'quote_quantity_max',
				array(
					'label'   => __( 'Maximum Quantity', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::NUMBER,
					'min'     => 1,
					'step'    => 1,
					'default' => intval( $this->default_text( 'quote_quantity_max', 12 ) ),
				)
			);
			$this->add_control(
				'quote_quantity_default',
				array(
					'label'   => __( 'Default Quantity', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::NUMBER,
					'min'     => 1,
					'step'    => 1,
					'default' => intval( $this->default_text( 'quote_quantity_default', 4 ) ),
				)
			);
			$this->add_control(
				'quote_currency',
				array(
					'label'   => __( 'Currency Prefix', 'niwa-ai' ),
					'type'    => \Elementor\Controls_Manager::TEXT,
					'default' => $this->default_text( 'quote_currency', '$' ),
				)
			);
		}

		$this->end_controls_section();

		$this->start_controls_section(
			'niwa_style',
			array(
				'label' => __( 'Style', 'niwa-ai' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'background_color',
			array(
				'label'   => __( 'Background', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['background_color'] ) ? (string) $config['background_color'] : '#07100d',
			)
		);
		$this->add_control(
			'text_color',
			array(
				'label'   => __( 'Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['text_color'] ) ? (string) $config['text_color'] : '#f8fafc',
			)
		);
		$this->add_control(
			'accent_color',
			array(
				'label'   => __( 'Accent', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['accent_color'] ) ? (string) $config['accent_color'] : '#d6b46a',
			)
		);
		$this->add_control(
			'card_background',
			array(
				'label'   => __( 'Card Background', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['card_background'] ) ? (string) $config['card_background'] : '#ffffff',
			)
		);
		$this->add_control(
			'card_text_color',
			array(
				'label'   => __( 'Card Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['card_text_color'] ) ? (string) $config['card_text_color'] : '#111827',
			)
		);
		$this->add_control(
			'muted_color',
			array(
				'label'   => __( 'Muted Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['muted_color'] ) ? (string) $config['muted_color'] : '#64748b',
			)
		);
		$this->add_control(
			'button_background',
			array(
				'label'   => __( 'Button Background', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['button_background'] ) ? (string) $config['button_background'] : '#d6b46a',
			)
		);
		$this->add_control(
			'button_text_color',
			array(
				'label'   => __( 'Button Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['button_text_color'] ) ? (string) $config['button_text_color'] : '#07100d',
			)
		);
		$this->add_control(
			'selected_card_background',
			array(
				'label'   => __( 'Selected Card Background', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['selected_card_background'] ) ? (string) $config['selected_card_background'] : '#07100d',
			)
		);
		$this->add_control(
			'selected_card_text_color',
			array(
				'label'   => __( 'Selected Card Text', 'niwa-ai' ),
				'type'    => \Elementor\Controls_Manager::COLOR,
				'default' => isset( $config['selected_card_text_color'] ) ? (string) $config['selected_card_text_color'] : '#ffffff',
			)
		);
		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$kind     = $this->kind();
		echo '<section class="niwa-native-widget niwa-native-widget--' . esc_attr( $kind ) . '" data-niwa-native-widget="' . esc_attr( (string) $this->niwa_snippet_id() ) . '" data-niwa-kind="' . esc_attr( $kind ) . '" style="' . esc_attr( $this->style_vars( $settings ) ) . '">';
		switch ( $kind ) {
			case 'team_accordion':
				$this->render_team_accordion( $settings );
				break;
			case 'testimonial_carousel':
				$this->render_testimonial_carousel( $settings );
				break;
			case 'pricing_cards':
				$this->render_pricing_cards( $settings );
				break;
			case 'product_finder_quiz':
				$this->render_product_finder_quiz( $settings );
				break;
			case 'quote_calculator':
				$this->render_quote_calculator( $settings );
				break;
			case 'habit_tracker':
				$this->render_habit_tracker( $settings );
				break;
			case 'image_reveal_menu':
			default:
				$this->render_image_reveal_menu( $settings );
				break;
		}
		echo '</section>';
	}

	protected function content_template() {
		?>
		<section class="niwa-native-widget niwa-native-widget--editor">
			<div class="niwa-native-widget__intro">
				<span>{{{ settings.eyebrow }}}</span>
				<h2>{{{ settings.title }}}</h2>
				<p>{{{ settings.intro }}}</p>
			</div>
		</section>
		<?php
	}

	private function render_image_reveal_menu( $settings ) {
		$items = $this->settings_items( $settings );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-reveal-native__interactive"><div class="niwa-reveal-native__items" role="tablist">';
		foreach ( $items as $index => $item ) {
			echo '<button type="button" class="niwa-reveal-native__item' . ( 0 === $index ? ' is-active' : '' ) . '" data-niwa-reveal-index="' . esc_attr( (string) $index ) . '" aria-selected="' . ( 0 === $index ? 'true' : 'false' ) . '">';
			echo '<span>' . esc_html( str_pad( (string) ( $index + 1 ), 2, '0', STR_PAD_LEFT ) ) . '</span><strong>' . esc_html( $item['label'] ) . '</strong><em>' . esc_html( $item['description'] ) . '</em></button>';
		}
		echo '</div><div class="niwa-reveal-native__stage">';
		foreach ( $items as $index => $item ) {
			$image = '' !== $item['image'] ? $item['image'] : 'https://images.unsplash.com/photo-1604908176997-125f25cc6f3d?auto=format&fit=crop&w=1200&q=80';
			echo '<figure class="niwa-reveal-native__image' . ( 0 === $index ? ' is-active' : '' ) . '" data-niwa-reveal-panel="' . esc_attr( (string) $index ) . '" style="background-image:linear-gradient(140deg,rgba(2,12,10,.16),rgba(2,12,10,.66)),url(' . esc_url( $image ) . ')"><figcaption>' . esc_html( $item['label'] ) . '</figcaption></figure>';
		}
		echo '</div></div>';
	}

	private function render_team_accordion( $settings ) {
		$items = $this->settings_items( $settings );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-team-native__cards">';
		foreach ( $items as $index => $item ) {
			$image = '' !== $item['image'] ? $item['image'] : 'https://images.unsplash.com/photo-1527980965255-d3b416303d12?auto=format&fit=crop&w=1200&q=80';
			echo '<article class="niwa-team-native__card' . ( 0 === $index ? ' is-active' : '' ) . '" data-niwa-team-card tabindex="0" aria-expanded="' . ( 0 === $index ? 'true' : 'false' ) . '" style="background-image:linear-gradient(180deg,rgba(7,16,13,.05),rgba(7,16,13,.75)),url(' . esc_url( $image ) . ')">';
			echo '<div><span>' . esc_html( str_pad( (string) ( $index + 1 ), 2, '0', STR_PAD_LEFT ) ) . '</span><h3>' . esc_html( $item['label'] ) . '</h3><p>' . esc_html( $item['description'] ) . '</p></div></article>';
		}
		echo '</div>';
	}

	private function render_testimonial_carousel( $settings ) {
		$items = isset( $settings['testimonials'] ) && is_array( $settings['testimonials'] ) ? $settings['testimonials'] : $this->default_testimonials( $this->defaults() );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-testimonials-native__track">';
		foreach ( array_values( $items ) as $index => $item ) {
			$content = isset( $item['testimonial_content'] ) ? (string) $item['testimonial_content'] : '';
			$name    = isset( $item['testimonial_name'] ) ? (string) $item['testimonial_name'] : '';
			$role    = isset( $item['testimonial_role'] ) ? (string) $item['testimonial_role'] : '';
			$rating  = max( 1, min( 5, intval( isset( $item['testimonial_rating'] ) ? $item['testimonial_rating'] : 5 ) ) );
			echo '<article class="' . ( 0 === $index ? 'is-active' : '' ) . '" data-niwa-testimonial-slide="' . esc_attr( (string) $index ) . '">';
			echo '<div class="niwa-testimonials-native__stars" aria-label="' . esc_attr( (string) $rating ) . ' out of 5 stars">' . wp_kses_post( str_repeat( '&#9733;', $rating ) ) . '</div>';
			echo '<p>' . esc_html( $content ) . '</p><strong>' . esc_html( $name ) . '</strong><span>' . esc_html( $role ) . '</span></article>';
		}
		echo '</div><div class="niwa-testimonials-native__controls"><button type="button" data-niwa-testimonial-prev aria-label="' . esc_attr__( 'Previous testimonial', 'niwa-ai' ) . '">&lt;</button><output data-niwa-testimonial-output>Slide 1 of ' . esc_html( (string) max( 1, count( $items ) ) ) . '</output><button type="button" data-niwa-testimonial-next aria-label="' . esc_attr__( 'Next testimonial', 'niwa-ai' ) . '">&gt;</button></div>';
	}

	private function render_pricing_cards( $settings ) {
		$plans = isset( $settings['plans'] ) && is_array( $settings['plans'] ) ? $settings['plans'] : $this->default_plans( $this->defaults() );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '<output data-niwa-pricing-output>' . esc_html__( 'Select a plan.', 'niwa-ai' ) . '</output></div><div class="niwa-pricing-native__grid">';
		foreach ( array_values( $plans ) as $index => $plan ) {
			$title       = isset( $plan['plan_title'] ) ? (string) $plan['plan_title'] : __( 'Plan', 'niwa-ai' );
			$price       = isset( $plan['plan_price'] ) ? (string) $plan['plan_price'] : '';
			$features    = isset( $plan['plan_features'] ) ? (string) $plan['plan_features'] : '';
			$button      = isset( $plan['plan_button'] ) && '' !== trim( (string) $plan['plan_button'] ) ? (string) $plan['plan_button'] : __( 'Choose plan', 'niwa-ai' );
			$highlighted = ( isset( $plan['plan_highlighted'] ) && 'yes' === (string) $plan['plan_highlighted'] ) || 1 === $index;
			echo '<article class="' . ( $highlighted ? 'is-selected' : '' ) . '" data-niwa-plan="' . esc_attr( $title ) . '"><h3>' . esc_html( $title ) . '</h3><strong>' . esc_html( $price ) . '</strong><ul>';
			foreach ( preg_split( '/\r\n|\r|\n|,/', $features ) as $feature ) {
				$feature = trim( (string) $feature );
				if ( '' !== $feature ) {
					echo '<li>' . esc_html( $feature ) . '</li>';
				}
			}
			echo '</ul><button type="button" data-niwa-plan-select>' . esc_html( $button ) . '</button></article>';
		}
		echo '</div>';
	}

	private function render_product_finder_quiz( $settings ) {
		$items = $this->settings_items( $settings );
		if ( empty( $items ) ) {
			$items = array(
				array( 'label' => 'Gift experience', 'description' => 'Polished option for first-time buyers.', 'image' => '' ),
				array( 'label' => 'Everyday pantry', 'description' => 'Useful bundle for repeat use.', 'image' => '' ),
				array( 'label' => 'Chef-grade ingredient', 'description' => 'Premium option for experienced users.', 'image' => '' ),
			);
		}
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-product-quiz-native" data-niwa-product-quiz>';
		echo '<div class="niwa-product-quiz-native__choices">';
		foreach ( array_values( $items ) as $index => $item ) {
			echo '<button type="button" class="' . ( 0 === $index ? 'is-active' : '' ) . '" data-niwa-product-choice="' . esc_attr( (string) $index ) . '" data-result-title="' . esc_attr( $item['label'] ) . '" data-result-copy="' . esc_attr( $item['description'] ) . '">';
			echo '<span>' . esc_html( str_pad( (string) ( $index + 1 ), 2, '0', STR_PAD_LEFT ) ) . '</span><strong>' . esc_html( $item['label'] ) . '</strong><em>' . esc_html( $item['description'] ) . '</em></button>';
		}
		$first = $items[0];
		echo '</div><aside><small>' . esc_html__( 'Best match', 'niwa-ai' ) . '</small><strong data-niwa-product-result-title>' . esc_html( $first['label'] ) . '</strong><p data-niwa-product-result-copy>' . esc_html( $first['description'] ) . '</p>';
		echo '<a href="' . esc_url( $this->url_setting( $settings, 'cta_url', '#' ) ) . '">' . esc_html( $this->setting( $settings, 'cta_text', __( 'Explore recommendation', 'niwa-ai' ) ) ) . '</a></aside></div>';
	}

	private function render_quote_calculator( $settings ) {
		$unit     = max( 1, intval( isset( $settings['quote_unit_price'] ) ? $settings['quote_unit_price'] : 80 ) );
		$min      = max( 1, intval( isset( $settings['quote_quantity_min'] ) ? $settings['quote_quantity_min'] : 1 ) );
		$max      = max( $min, intval( isset( $settings['quote_quantity_max'] ) ? $settings['quote_quantity_max'] : 12 ) );
		$default  = max( $min, min( $max, intval( isset( $settings['quote_quantity_default'] ) ? $settings['quote_quantity_default'] : 4 ) ) );
		$currency = $this->setting( $settings, 'quote_currency', '$' );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-quote-native" data-niwa-quote-calc data-unit-price="' . esc_attr( (string) $unit ) . '" data-currency="' . esc_attr( $currency ) . '">';
		echo '<label>' . esc_html__( 'Quantity', 'niwa-ai' ) . '<input type="range" min="' . esc_attr( (string) $min ) . '" max="' . esc_attr( (string) $max ) . '" value="' . esc_attr( (string) $default ) . '" data-niwa-quote-qty><output data-niwa-quote-qty-output>' . esc_html( (string) $default ) . '</output></label>';
		echo '<div class="niwa-quote-native__total"><span>' . esc_html__( 'Estimated total', 'niwa-ai' ) . '</span><strong data-niwa-quote-total>' . esc_html( $currency . ( $default * $unit ) ) . '</strong></div>';
		echo '<a href="' . esc_url( $this->url_setting( $settings, 'cta_url', '#' ) ) . '">' . esc_html( $this->setting( $settings, 'cta_text', __( 'Request quote', 'niwa-ai' ) ) ) . '</a></div>';
	}

	private function render_habit_tracker( $settings ) {
		$items = $this->settings_items( $settings );
		if ( empty( $items ) ) {
			$items = array(
				array( 'label' => 'Plan', 'description' => 'Map the next action.', 'image' => '' ),
				array( 'label' => 'Create', 'description' => 'Produce the deliverable.', 'image' => '' ),
				array( 'label' => 'Publish', 'description' => 'Ship the update.', 'image' => '' ),
				array( 'label' => 'Follow up', 'description' => 'Close the loop.', 'image' => '' ),
				array( 'label' => 'Review', 'description' => 'Check quality and results.', 'image' => '' ),
			);
		}
		$days = array( 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun' );
		echo '<div class="niwa-native-widget__intro">';
		$this->render_intro( $settings );
		echo '</div><div class="niwa-habit-native" data-niwa-habit-tracker>';
		echo '<div class="niwa-habit-native__stats"><output data-niwa-habit-progress>0% complete</output><output data-niwa-habit-streak>0 day streak</output><button type="button" data-niwa-habit-reset>' . esc_html__( 'Reset', 'niwa-ai' ) . '</button></div>';
		echo '<div class="niwa-habit-native__grid" role="grid" aria-label="' . esc_attr__( 'Weekly habit tracker', 'niwa-ai' ) . '"><span></span>';
		foreach ( $days as $day ) {
			echo '<strong>' . esc_html( $day ) . '</strong>';
		}
		foreach ( array_slice( $items, 0, 8 ) as $row => $item ) {
			echo '<b>' . esc_html( $item['label'] ) . '</b>';
			foreach ( $days as $day_index => $day ) {
				$pressed = $row < 2 && $day_index < 2;
				echo '<button type="button" data-niwa-habit-cell data-day="' . esc_attr( (string) $day_index ) . '" aria-pressed="' . ( $pressed ? 'true' : 'false' ) . '" aria-label="' . esc_attr( $item['label'] . ' ' . $day ) . '"' . ( $pressed ? ' class="is-complete"' : '' ) . '><span></span></button>';
			}
		}
		echo '</div></div>';
	}

	private function render_intro( $settings ) {
		$cta_url = $this->url_setting( $settings, 'cta_url', '#' );
		echo '<span>' . esc_html( $this->setting( $settings, 'eyebrow', 'Explore' ) ) . '</span>';
		echo '<h2>' . esc_html( $this->setting( $settings, 'title', $this->get_title() ) ) . '</h2>';
		$intro = $this->setting( $settings, 'intro', '' );
		if ( '' !== $intro ) {
			echo '<p>' . esc_html( $intro ) . '</p>';
		}
		echo '<a class="niwa-native-widget__cta" href="' . esc_url( $cta_url ) . '">' . esc_html( $this->setting( $settings, 'cta_text', 'Learn more' ) ) . '</a>';
	}

	private function config() {
		$config = $this->niwa_template_config();
		return is_array( $config ) ? $config : array();
	}

	private function kind() {
		$config = $this->config();
		return isset( $config['kind'] ) ? sanitize_key( (string) $config['kind'] ) : 'image_reveal_menu';
	}

	private function defaults() {
		$config = $this->config();
		return isset( $config['defaults'] ) && is_array( $config['defaults'] ) ? $config['defaults'] : array();
	}

	private function default_text( $key, $fallback ) {
		$defaults = $this->defaults();
		return isset( $defaults[ $key ] ) && '' !== trim( (string) $defaults[ $key ] ) ? (string) $defaults[ $key ] : (string) $fallback;
	}

	private function default_repeater_items( $defaults ) {
		$items = isset( $defaults['items'] ) && is_array( $defaults['items'] ) ? $defaults['items'] : array();
		if ( empty( $items ) ) {
			$items = array(
				array( 'label' => 'First item', 'description' => 'Short supporting copy.', 'image_url' => '' ),
				array( 'label' => 'Second item', 'description' => 'Short supporting copy.', 'image_url' => '' ),
				array( 'label' => 'Third item', 'description' => 'Short supporting copy.', 'image_url' => '' ),
			);
		}
		return array_map(
			function ( $item ) {
				return array(
					'item_label'       => isset( $item['label'] ) ? (string) $item['label'] : 'Item',
					'item_description' => isset( $item['description'] ) ? (string) $item['description'] : '',
					'item_image'       => array( 'url' => isset( $item['image_url'] ) ? (string) $item['image_url'] : '' ),
				);
			},
			array_slice( $items, 0, 8 )
		);
	}

	private function default_testimonials( $defaults ) {
		$items = isset( $defaults['items'] ) && is_array( $defaults['items'] ) ? $defaults['items'] : array();
		if ( empty( $items ) ) {
			$items = array(
				array( 'content' => 'Clear, fast and useful from the first session.', 'name' => 'Customer', 'role' => 'Verified customer', 'rating' => 5 ),
				array( 'content' => 'The page finally explains the offer properly.', 'name' => 'Founder', 'role' => 'Founder', 'rating' => 5 ),
				array( 'content' => 'The section matched our offer and tone.', 'name' => 'Operator', 'role' => 'Operator', 'rating' => 5 ),
			);
		}
		return array_map(
			function ( $item ) {
				return array(
					'testimonial_content' => isset( $item['content'] ) ? (string) $item['content'] : '',
					'testimonial_name'    => isset( $item['name'] ) ? (string) $item['name'] : 'Customer',
					'testimonial_role'    => isset( $item['role'] ) ? (string) $item['role'] : 'Verified customer',
					'testimonial_rating'  => isset( $item['rating'] ) ? intval( $item['rating'] ) : 5,
				);
			},
			array_slice( $items, 0, 8 )
		);
	}

	private function default_plans( $defaults ) {
		$plans = isset( $defaults['plans'] ) && is_array( $defaults['plans'] ) ? $defaults['plans'] : array();
		if ( empty( $plans ) ) {
			$plans = array(
				array( 'title' => 'Starter', 'price' => '$49', 'features' => "Fast launch\nCore sections\nBasic support" ),
				array( 'title' => 'Growth', 'price' => '$149', 'features' => "Custom layout\nAutomations\nPriority support", 'highlighted' => true ),
				array( 'title' => 'Scale', 'price' => 'Custom', 'features' => "Advanced workflows\nIntegrations\nDedicated support" ),
			);
		}
		return array_map(
			function ( $plan ) {
				return array(
					'plan_title'       => isset( $plan['title'] ) ? (string) $plan['title'] : 'Plan',
					'plan_price'       => isset( $plan['price'] ) ? (string) $plan['price'] : '',
					'plan_features'    => isset( $plan['features'] ) ? (string) $plan['features'] : '',
					'plan_button'      => isset( $plan['button'] ) ? (string) $plan['button'] : __( 'Choose plan', 'niwa-ai' ),
					'plan_highlighted' => ! empty( $plan['highlighted'] ) ? 'yes' : '',
				);
			},
			array_slice( $plans, 0, 6 )
		);
	}

	private function settings_items( $settings ) {
		$items = isset( $settings['items'] ) && is_array( $settings['items'] ) ? $settings['items'] : $this->default_repeater_items( $this->defaults() );
		return array_map(
			function ( $item ) {
				$image = '';
				if ( isset( $item['item_image']['url'] ) ) {
					$image = (string) $item['item_image']['url'];
				}
				return array(
					'label'       => isset( $item['item_label'] ) ? (string) $item['item_label'] : 'Item',
					'description' => isset( $item['item_description'] ) ? (string) $item['item_description'] : '',
					'image'       => $image,
				);
			},
			array_values( $items )
		);
	}

	private function setting( $settings, $key, $fallback ) {
		return isset( $settings[ $key ] ) && '' !== trim( (string) $settings[ $key ] ) ? (string) $settings[ $key ] : (string) $fallback;
	}

	private function url_setting( $settings, $key, $fallback ) {
		if ( isset( $settings[ $key ]['url'] ) && '' !== trim( (string) $settings[ $key ]['url'] ) ) {
			return (string) $settings[ $key ]['url'];
		}
		if ( isset( $settings[ $key ] ) && is_string( $settings[ $key ] ) && '' !== trim( $settings[ $key ] ) ) {
			return (string) $settings[ $key ];
		}
		return (string) $fallback;
	}

	private function style_vars( $settings ) {
		$vars = array(
			'--niwa-native-bg'          => $this->setting( $settings, 'background_color', '#07100d' ),
			'--niwa-native-text'        => $this->setting( $settings, 'text_color', '#f8fafc' ),
			'--niwa-native-accent'      => $this->setting( $settings, 'accent_color', '#d6b46a' ),
			'--niwa-native-card'        => $this->setting( $settings, 'card_background', '#ffffff' ),
			'--niwa-native-card-text'   => $this->setting( $settings, 'card_text_color', '#111827' ),
			'--niwa-native-muted'       => $this->setting( $settings, 'muted_color', '#64748b' ),
			'--niwa-native-button-bg'   => $this->setting( $settings, 'button_background', '#d6b46a' ),
			'--niwa-native-button-text' => $this->setting( $settings, 'button_text_color', '#07100d' ),
			'--niwa-native-selected-card' => $this->setting( $settings, 'selected_card_background', '#07100d' ),
			'--niwa-native-selected-text' => $this->setting( $settings, 'selected_card_text_color', '#ffffff' ),
		);
		$out = '';
		foreach ( $vars as $name => $value ) {
			$out .= $name . ':' . sanitize_hex_color( $value ) . ';';
		}
		return $out;
	}
}
