<?php

namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;

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

/**
 * Renders layered feature-list layouts with badges, support copy and optional connectors.
 */
class FeatureListWidget extends BaseWidget
{
    public function get_name(): string
    {
        return 'calio-feature-list';
    }

    public function get_title(): string
    {
        return esc_html__('Feature List', 'calio-addon');
    }

    public function get_icon(): string
    {
        return 'eicon-bullet-list';
    }

    public function get_style_depends(): array
    {
        return $this->resolve_widget_style_depends('calio-widget-feature-list');
    }

    protected function register_controls(): void
    {
        $this->start_controls_section('layout_section', ['label' => esc_html__('Layout', 'calio-addon')]);
        $this->add_control('layout_skin', [
            'label' => esc_html__('Layout Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'stacked',
            'options' => [
                'stacked' => esc_html__('Stacked', 'calio-addon'),
                'cards' => esc_html__('Cards', 'calio-addon'),
                'editorial' => esc_html__('Editorial', 'calio-addon'),
                'split' => esc_html__('Split', 'calio-addon'),
            ],
        ]);
        $this->add_control('featured_preset', [
            'label' => esc_html__('Featured Preset', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'soft',
            'options' => [
                'soft' => esc_html__('Soft', 'calio-addon'),
                'spotlight' => esc_html__('Spotlight', 'calio-addon'),
                'contrast' => esc_html__('Contrast', 'calio-addon'),
            ],
        ]);
        $this->add_control('item_motion', [
            'label' => esc_html__('Item Motion', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'soft',
            'options' => [
                'none' => esc_html__('None', 'calio-addon'),
                'soft' => esc_html__('Soft', 'calio-addon'),
                'lift' => esc_html__('Lift', 'calio-addon'),
            ],
        ]);
        $this->add_control('media_hover_skin', [
            'label' => esc_html__('Media Hover Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'zoom',
            'options' => [
                'none' => esc_html__('None', 'calio-addon'),
                'zoom' => esc_html__('Zoom', 'calio-addon'),
                'tilt' => esc_html__('Tilt', 'calio-addon'),
            ],
        ]);
        $this->add_control('show_media_ring', [
            'label' => esc_html__('Show Media Ring', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('media_position', [
            'label' => esc_html__('Media Position', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'left',
            'options' => [
                'left' => esc_html__('Left', 'calio-addon'),
                'top' => esc_html__('Top', 'calio-addon'),
            ],
        ]);
        $this->add_control('columns', [
            'label' => esc_html__('Columns', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => '1',
            'options' => [
                '1' => '1',
                '2' => '2',
                '3' => '3',
            ],
            'condition' => ['layout_skin' => 'cards'],
        ]);
        $this->add_control('show_connector', [
            'label' => esc_html__('Show Connector', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('connector_skin', [
            'label' => esc_html__('Connector Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'line',
            'options' => [
                'line' => esc_html__('Line', 'calio-addon'),
                'dashed' => esc_html__('Dashed', 'calio-addon'),
                'glow' => esc_html__('Glow', 'calio-addon'),
            ],
            'condition' => ['show_connector' => 'yes'],
        ]);
        $this->add_control('show_numbers', [
            'label' => esc_html__('Show Numbers', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $this->add_control('show_badges', [
            'label' => esc_html__('Show Badges', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_support_note', [
            'label' => esc_html__('Show Support Note', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_support_chip', [
            'label' => esc_html__('Show Support Chip', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_featured_marker', [
            'label' => esc_html__('Show Featured Marker', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('featured_marker_text', [
            'label' => esc_html__('Featured Marker Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Featured', 'calio-addon'),
            'condition' => ['show_featured_marker' => 'yes'],
        ]);
        $this->add_control('show_checklist', [
            'label' => esc_html__('Show Checklist', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_link_arrow', [
            'label' => esc_html__('Show Link Arrow', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->end_controls_section();

        $this->start_controls_section('items_section', ['label' => esc_html__('Items', 'calio-addon')]);
        $repeater = new Repeater();
        $repeater->add_control('media_type', [
            'label' => esc_html__('Media Type', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'icon',
            'options' => [
                'icon' => esc_html__('Icon', 'calio-addon'),
                'image' => esc_html__('Image', 'calio-addon'),
                'number' => esc_html__('Number', 'calio-addon'),
            ],
        ]);
        $repeater->add_control('icon', [
            'label' => esc_html__('Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-check',
                'library' => 'fa-solid',
            ],
            'condition' => ['media_type' => 'icon'],
        ]);
        $repeater->add_control('image', [
            'label' => esc_html__('Image', 'calio-addon'),
            'type' => Controls_Manager::MEDIA,
            'condition' => ['media_type' => 'image'],
        ]);
        $repeater->add_control('eyebrow', [
            'label' => esc_html__('Eyebrow', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
        ]);
        $repeater->add_control('title', [
            'label' => esc_html__('Title', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Feature Title', 'calio-addon'),
            'label_block' => true,
        ]);
        $repeater->add_control('description', [
            'label' => esc_html__('Description', 'calio-addon'),
            'type' => Controls_Manager::TEXTAREA,
            'default' => esc_html__('Describe the benefit, workflow or product capability in a tighter, more editorial way.', 'calio-addon'),
        ]);
        $repeater->add_control('badge_text', [
            'label' => esc_html__('Badge', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
        ]);
        $repeater->add_control('overlay_label', [
            'label' => esc_html__('Media Overlay Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
            'condition' => ['media_type' => 'image'],
        ]);
        $repeater->add_control('support_note', [
            'label' => esc_html__('Support Note', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
        ]);
        $repeater->add_control('support_chip', [
            'label' => esc_html__('Support Chip', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => '',
        ]);
        $repeater->add_control('checklist_items', [
            'label' => esc_html__('Checklist Items', 'calio-addon'),
            'type' => Controls_Manager::TEXTAREA,
            'default' => '',
            'description' => esc_html__('Optional line-separated checklist items.', 'calio-addon'),
        ]);
        $repeater->add_control('is_featured', [
            'label' => esc_html__('Featured Item', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $repeater->add_control('link', [
            'label' => esc_html__('Item Link', 'calio-addon'),
            'type' => Controls_Manager::URL,
            'default' => ['url' => ''],
        ]);

        $this->add_control('items', [
            'label' => esc_html__('Feature Items', 'calio-addon'),
            'type' => Controls_Manager::REPEATER,
            'fields' => $repeater->get_controls(),
            'title_field' => '{{{ title }}}',
            'default' => [
                [
                    'media_type' => 'icon',
                    'eyebrow' => 'Workflow',
                    'title' => 'Design with a clearer hierarchy',
                    'description' => 'Combine headline, support copy and badge treatments in a more deliberate feature row.',
                    'badge_text' => 'Core',
                    'overlay_label' => 'Preview',
                    'support_note' => 'Great for section intros and product explainers.',
                    'support_chip' => 'Hero Ready',
                    'checklist_items' => "Layered badge row\nSupport copy under the main title",
                    'is_featured' => 'yes',
                ],
                [
                    'media_type' => 'number',
                    'eyebrow' => 'System',
                    'title' => 'Build repeatable content patterns',
                    'description' => 'Turn simple bullet rows into a more branded system with connectors, notes and richer media.',
                    'badge_text' => 'Library',
                    'support_note' => 'Works well for roadmap, onboarding and product value stacks.',
                    'support_chip' => 'Reusable',
                    'checklist_items' => "Reusable content rows\nCleaner process storytelling",
                ],
                [
                    'media_type' => 'icon',
                    'eyebrow' => 'Output',
                    'title' => 'Keep the layout expressive',
                    'description' => 'Switch between stacked, card and editorial skins without rebuilding the content structure.',
                    'badge_text' => 'Flexible',
                    'support_note' => 'Useful for services, pricing highlights or feature comparisons.',
                    'support_chip' => 'Adaptive',
                    'checklist_items' => "Stacked, cards and editorial presets\nOptional per-item link affordance",
                ],
            ],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_items', ['label' => esc_html__('Items', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('feature_list_item_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('feature_list_item_surface_tabs');
        $this->start_controls_tab('feature_list_item_surface_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_background_type_control('feature_list_item_bg', '{{WRAPPER}} .calio-feature-list-item');
        $this->add_border_paint_controls('feature_list_item_border_color', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item');
        $this->end_controls_tab();
        $this->start_controls_tab('feature_list_item_surface_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_background_type_control('feature_list_item_bg_hover', '{{WRAPPER}} .calio-feature-list-item:hover');
        $this->add_border_paint_controls('feature_list_item_border_color_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item:hover');
        $this->end_controls_tab();
        $this->start_controls_tab('feature_list_item_surface_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_background_type_control('feature_list_item_bg_active', '{{WRAPPER}} .calio-feature-list-item:focus-within');
        $this->add_border_paint_controls('feature_list_item_border_color_active', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item:focus-within');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('feature_list_item_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_item_border', 'selector' => '{{WRAPPER}} .calio-feature-list-item']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'feature_list_item_shadow', 'selector' => '{{WRAPPER}} .calio-feature-list-item']);
        $this->add_style_heading('feature_list_item_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('feature_list_item_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('feature_list_item_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item', 'padding');
        $this->add_control('feature_list_item_gap', [
            'label' => esc_html__('Item Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 0, 'max' => 48]],
            'default' => ['size' => 18],
            'selectors' => ['{{WRAPPER}} .calio-feature-list' => 'gap: {{SIZE}}{{UNIT}};'],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_title', ['label' => esc_html__('Title & Copy', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_title_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-title']);
        $this->add_text_paint_controls('feature_list_title_text', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-title');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_desc_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-description']);
        $this->add_text_paint_controls('feature_list_desc_text', esc_html__('Description Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-description');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_support_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-support']);
        $this->add_text_paint_controls('feature_list_support_text', esc_html__('Support Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-support');
        $this->end_controls_section();

        $this->start_controls_section('style_meta', ['label' => esc_html__('Meta & Media', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_eyebrow_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-eyebrow']);
        $this->add_text_paint_controls('feature_list_eyebrow_text', esc_html__('Eyebrow Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-eyebrow');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_badge_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-badge']);
        $this->add_text_paint_controls('feature_list_badge_text', esc_html__('Badge Text Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-badge');
        $this->add_background_type_control('feature_list_badge_bg', '{{WRAPPER}} .calio-feature-list-badge');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_badge_border', 'selector' => '{{WRAPPER}} .calio-feature-list-badge']);
        $this->add_dimensions_control('feature_list_badge_radius', esc_html__('Badge Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-badge', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_text_paint_controls('feature_list_media_text', esc_html__('Media Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-media, {{WRAPPER}} .calio-feature-list-number');
        $this->add_background_type_control('feature_list_media_bg', '{{WRAPPER}} .calio-feature-list-media');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_media_border', 'selector' => '{{WRAPPER}} .calio-feature-list-media']);
        $this->add_dimensions_control('feature_list_media_radius', esc_html__('Media Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-media, {{WRAPPER}} .calio-feature-list-media img', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_background_paint_controls('feature_list_connector_color', esc_html__('Connector Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-item::after');
        $this->end_controls_section();

        $this->start_controls_section('style_checklist', ['label' => esc_html__('Checklist', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_checklist_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-checklist']);
        $this->add_text_paint_controls('feature_list_checklist_text', esc_html__('Checklist Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-checklist');
        $this->add_text_paint_controls('feature_list_check_icon_text', esc_html__('Check Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-checklist li::before');
        $this->add_control('feature_list_checklist_gap', [
            'label' => esc_html__('Checklist Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 0, 'max' => 24]],
            'default' => ['size' => 8],
            'selectors' => ['{{WRAPPER}} .calio-feature-list-checklist' => 'gap: {{SIZE}}{{UNIT}};'],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_link', ['label' => esc_html__('Link Affordance', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_text_paint_controls('feature_list_link_arrow_text', esc_html__('Arrow Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-link-arrow');
        $this->end_controls_section();

        $this->start_controls_section('style_support_chip', ['label' => esc_html__('Support Chip', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_support_chip_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-support-chip']);
        $this->add_text_paint_controls('feature_list_support_chip_text', esc_html__('Chip Text Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-support-chip');
        $this->add_background_type_control('feature_list_support_chip_bg', '{{WRAPPER}} .calio-feature-list-support-chip');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_support_chip_border', 'selector' => '{{WRAPPER}} .calio-feature-list-support-chip']);
        $this->add_dimensions_control('feature_list_support_chip_radius', esc_html__('Chip Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-support-chip', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_overlay_label', ['label' => esc_html__('Overlay Label', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_overlay_label_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-overlay-label']);
        $this->add_text_paint_controls('feature_list_overlay_label_text', esc_html__('Label Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-overlay-label');
        $this->add_background_type_control('feature_list_overlay_label_bg', '{{WRAPPER}} .calio-feature-list-overlay-label');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_overlay_label_border', 'selector' => '{{WRAPPER}} .calio-feature-list-overlay-label']);
        $this->add_dimensions_control('feature_list_overlay_label_radius', esc_html__('Label Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-overlay-label', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_featured_marker', ['label' => esc_html__('Featured Marker', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'feature_list_featured_marker_typography', 'selector' => '{{WRAPPER}} .calio-feature-list-featured-marker']);
        $this->add_text_paint_controls('feature_list_featured_marker_text', esc_html__('Marker Text Color', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-featured-marker');
        $this->add_background_type_control('feature_list_featured_marker_bg', '{{WRAPPER}} .calio-feature-list-featured-marker');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'feature_list_featured_marker_border', 'selector' => '{{WRAPPER}} .calio-feature-list-featured-marker']);
        $this->add_dimensions_control('feature_list_featured_marker_radius', esc_html__('Marker Radius', 'calio-addon'), '{{WRAPPER}} .calio-feature-list-featured-marker', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();
    }

    protected function render(): void
    {
        $settings = $this->get_settings_for_display();
        $items = isset($settings['items']) && is_array($settings['items']) ? $settings['items'] : [];

        if ($items === []) {
            return;
        }

        $classes = [
            'calio-feature-list',
            'skin-' . sanitize_html_class((string) ($settings['layout_skin'] ?? 'stacked')),
            'media-' . sanitize_html_class((string) ($settings['media_position'] ?? 'left')),
            'columns-' . sanitize_html_class((string) ($settings['columns'] ?? '1')),
            'featured-' . sanitize_html_class((string) ($settings['featured_preset'] ?? 'soft')),
            'connector-' . sanitize_html_class((string) ($settings['connector_skin'] ?? 'line')),
            'motion-' . sanitize_html_class((string) ($settings['item_motion'] ?? 'soft')),
            'media-hover-' . sanitize_html_class((string) ($settings['media_hover_skin'] ?? 'zoom')),
        ];

        if (($settings['show_connector'] ?? 'yes') === 'yes') {
            $classes[] = 'has-connector';
        }

        if (($settings['show_numbers'] ?? '') === 'yes') {
            $classes[] = 'show-numbers';
        }

        if (($settings['show_link_arrow'] ?? 'yes') === 'yes') {
            $classes[] = 'show-link-arrow';
        }

        if (($settings['show_media_ring'] ?? 'yes') === 'yes') {
            $classes[] = 'show-media-ring';
        }

        echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';

        foreach ($items as $index => $item) {
            $item_classes = ['calio-feature-list-item'];

            if (($item['is_featured'] ?? '') === 'yes') {
                $item_classes[] = 'is-featured';
            }

            if (!empty($item['link']['url'])) {
                $item_classes[] = 'is-linked';
            }

            $media_type = (string) ($item['media_type'] ?? 'icon');

            echo '<div class="' . esc_attr(implode(' ', $item_classes)) . '">';
            if (($settings['show_featured_marker'] ?? 'yes') === 'yes' && ($item['is_featured'] ?? '') === 'yes' && !empty($settings['featured_marker_text'])) {
                echo '<span class="calio-feature-list-featured-marker">' . esc_html((string) $settings['featured_marker_text']) . '</span>';
            }
            echo '<div class="calio-feature-list-media-wrap">';
            echo '<span class="calio-feature-list-media">';
            echo wp_kses_post($this->render_media($media_type, $item, $index + 1));
            echo '</span>';
            if ($media_type === 'image' && !empty($item['overlay_label'])) {
                echo '<span class="calio-feature-list-overlay-label">' . esc_html((string) $item['overlay_label']) . '</span>';
            }
            echo '</div>';
            echo '<div class="calio-feature-list-content">';

            if (!empty($item['eyebrow']) || (($settings['show_badges'] ?? 'yes') === 'yes' && !empty($item['badge_text']))) {
                echo '<div class="calio-feature-list-meta">';
                if (!empty($item['eyebrow'])) {
                    echo '<span class="calio-feature-list-eyebrow">' . esc_html((string) $item['eyebrow']) . '</span>';
                }
                if (($settings['show_badges'] ?? 'yes') === 'yes' && !empty($item['badge_text'])) {
                    echo '<span class="calio-feature-list-badge">' . esc_html((string) $item['badge_text']) . '</span>';
                }
                echo '</div>';
            }

            $title_markup = '<span class="calio-feature-list-title-text">' . esc_html((string) ($item['title'] ?? '')) . '</span>';

            if (($settings['show_link_arrow'] ?? 'yes') === 'yes' && !empty($item['link']['url'])) {
                $title_markup .= '<span class="calio-feature-list-link-arrow" aria-hidden="true">&rarr;</span>';
            }

            if (!empty($item['link']['url'])) {
                $title_markup = $this->wrap_link($title_markup, $item['link'], 'calio-feature-list-title-link');
            }

            echo '<h3 class="calio-feature-list-title">' . wp_kses_post($title_markup) . '</h3>';

            if (!empty($item['description'])) {
                echo '<div class="calio-feature-list-description">' . esc_html((string) $item['description']) . '</div>';
            }

            if (($settings['show_support_note'] ?? 'yes') === 'yes' && !empty($item['support_note'])) {
                echo '<div class="calio-feature-list-support">' . esc_html((string) $item['support_note']) . '</div>';
            }

            if (($settings['show_support_chip'] ?? 'yes') === 'yes' && !empty($item['support_chip'])) {
                echo '<div class="calio-feature-list-support-chip">' . esc_html((string) $item['support_chip']) . '</div>';
            }

            $checklist_items = $this->parse_checklist_items((string) ($item['checklist_items'] ?? ''));
            if (($settings['show_checklist'] ?? 'yes') === 'yes' && $checklist_items !== []) {
                echo '<ul class="calio-feature-list-checklist">';
                foreach ($checklist_items as $checklist_item) {
                    echo '<li>' . esc_html($checklist_item) . '</li>';
                }
                echo '</ul>';
            }

            echo '</div>';
            echo '</div>';
        }

        echo '</div>';
    }

    /**
     * Renders the chosen media type for each feature item.
     *
     * @param array<string, mixed> $item
     */
    private function render_media(string $media_type, array $item, int $number): string
    {
        if ($media_type === 'image' && !empty($item['image']['url'])) {
            $url = (string) $item['image']['url'];
            $alt = get_post_meta((int) ($item['image']['id'] ?? 0), '_wp_attachment_image_alt', true);

            return '<img src="' . esc_url($url) . '" alt="' . esc_attr($alt ?: wp_basename($url)) . '">';
        }

        if ($media_type === 'number') {
            return '<span class="calio-feature-list-number">' . esc_html(str_pad((string) $number, 2, '0', STR_PAD_LEFT)) . '</span>';
        }

        return $this->get_icon_markup($item['icon'] ?? []);
    }

    /**
     * Wraps a markup fragment with a secure anchor tag.
     *
     * @param array<string, mixed> $link
     */
    private function wrap_link(string $content, array $link, string $class_name): string
    {
        $href = isset($link['url']) ? (string) $link['url'] : '';
        if ($href === '') {
            return $content;
        }

        $rel = [];

        if (!empty($link['nofollow'])) {
            $rel[] = 'nofollow';
        }

        if (!empty($link['is_external'])) {
            $rel[] = 'noopener';
            $rel[] = 'noreferrer';
        }

        $output = '<a class="' . esc_attr($class_name) . '" href="' . esc_url($href) . '"';
        if (!empty($link['is_external'])) {
            $output .= ' target="' . esc_attr('_blank') . '"';
        }
        if (!empty($rel)) {
            $output .= ' rel="' . esc_attr(implode(' ', array_unique($rel))) . '"';
        }

        return $output . '>' . $content . '</a>';
    }

    /**
     * Parses a line-separated checklist textarea into clean rows.
     *
     * @return string[]
     */
    private function parse_checklist_items(string $value): array
    {
        $parts = preg_split('/[\r\n]+/', $value);
        if (!is_array($parts)) {
            return [];
        }

        $items = [];

        foreach ($parts as $part) {
            $item = trim(wp_strip_all_tags((string) $part));
            if ($item === '') {
                continue;
            }

            $items[] = $item;
        }

        return $items;
    }
}
