<?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 a styled team-member card with social links and multiple card skins.
 */
class TeamMemberWidget extends BaseWidget
{
    public function get_name(): string
    {
        return 'calio-team-member';
    }

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

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

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

    protected function register_controls(): void
    {
        $this->start_controls_section('layout_section', ['label' => esc_html__('Layout', 'calio-addon')]);
        $this->add_control('skin', [
            'label' => esc_html__('Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'simple',
            'options' => [
                'simple' => esc_html__('Simple', 'calio-addon'),
                'overlay' => esc_html__('Overlay', 'calio-addon'),
                'centered' => esc_html__('Centered', 'calio-addon'),
                'social-bottom' => esc_html__('Social Bottom', 'calio-addon'),
                'social-right' => esc_html__('Social Right', 'calio-addon'),
            ],
        ]);
        $this->add_control('image_hover_skin', [
            'label' => esc_html__('Image Hover Skin', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'zoom',
            'options' => [
                'none' => esc_html__('None', 'calio-addon'),
                'zoom' => esc_html__('Zoom', 'calio-addon'),
                'grayscale' => esc_html__('Grayscale', 'calio-addon'),
            ],
        ]);
        $this->add_control('image_shape', [
            'label' => esc_html__('Image Shape', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'rounded',
            'options' => [
                'rounded' => esc_html__('Rounded', 'calio-addon'),
                'circle' => esc_html__('Circle', 'calio-addon'),
                'soft-blob' => esc_html__('Soft Blob', 'calio-addon'),
            ],
        ]);
        $this->add_control('show_socials', [
            'label' => esc_html__('Show Social Links', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_meta_row', [
            'label' => esc_html__('Show Meta Row', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_contact_link', [
            'label' => esc_html__('Show Contact Link', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_button', [
            'label' => esc_html__('Show Profile Button', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => 'yes',
        ]);
        $this->add_control('show_image_ring', [
            'label' => esc_html__('Show Image Ring', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $this->add_control('show_panel_glow', [
            'label' => esc_html__('Show Panel Glow', 'calio-addon'),
            'type' => Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default' => '',
        ]);
        $this->add_control('card_motion', [
            'label' => esc_html__('Card 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('social_reveal_motion', [
            'label' => esc_html__('Social Reveal', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'slide',
            'options' => [
                'fade' => esc_html__('Fade', 'calio-addon'),
                'slide' => esc_html__('Slide', 'calio-addon'),
                'stagger' => esc_html__('Stagger', 'calio-addon'),
            ],
            'condition' => ['show_socials' => 'yes'],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('image_section', ['label' => esc_html__('Image', 'calio-addon')]);
        $this->add_control('image', [
            'label' => esc_html__('Image', 'calio-addon'),
            'type' => Controls_Manager::MEDIA,
        ]);
        $this->add_control('floating_label', [
            'label' => esc_html__('Floating Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Team', 'calio-addon'),
        ]);
        $this->add_control('designation_badge', [
            'label' => esc_html__('Designation Badge', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Available for new projects', 'calio-addon'),
        ]);
        $this->end_controls_section();

        $this->start_controls_section('content_section', ['label' => esc_html__('Content', 'calio-addon')]);
        $this->add_control('name', [
            'label' => esc_html__('Name', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('John Doe', 'calio-addon'),
        ]);
        $this->add_control('name_tag', [
            'label' => esc_html__('Name HTML Tag', 'calio-addon'),
            'type' => Controls_Manager::SELECT,
            'default' => 'h3',
            'options' => [
                'h1' => 'H1',
                'h2' => 'H2',
                'h3' => 'H3',
                'h4' => 'H4',
                'div' => 'DIV',
                'span' => 'SPAN',
                'p' => 'P',
            ],
        ]);
        $this->add_control('role', [
            'label' => esc_html__('Role', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Software Engineer', 'calio-addon'),
        ]);
        $this->add_control('info_chip', [
            'label' => esc_html__('Info Chip', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Product Design', 'calio-addon'),
        ]);
        $this->add_control('meta_label', [
            'label' => esc_html__('Meta Label', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Projects', 'calio-addon'),
            'condition' => ['show_meta_row' => 'yes'],
        ]);
        $this->add_control('meta_value', [
            'label' => esc_html__('Meta Value', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('42+', 'calio-addon'),
            'condition' => ['show_meta_row' => 'yes'],
        ]);
        $this->add_control('bio', [
            'label' => esc_html__('Bio', 'calio-addon'),
            'type' => Controls_Manager::TEXTAREA,
            'default' => esc_html__('Use a cleaner team card layout for personal intros, department sections or agency team pages.', 'calio-addon'),
        ]);
        $this->add_control('contact_link_text', [
            'label' => esc_html__('Contact Link Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('Say Hello', 'calio-addon'),
            'condition' => ['show_contact_link' => 'yes'],
        ]);
        $this->add_control('contact_link_url', [
            'label' => esc_html__('Contact Link URL', 'calio-addon'),
            'type' => Controls_Manager::URL,
            'default' => ['url' => ''],
            'condition' => ['show_contact_link' => 'yes'],
        ]);
        $this->add_control('profile_button_text', [
            'label' => esc_html__('Profile Button Text', 'calio-addon'),
            'type' => Controls_Manager::TEXT,
            'default' => esc_html__('View Profile', 'calio-addon'),
            'condition' => ['show_button' => 'yes'],
        ]);
        $this->add_control('profile_button_url', [
            'label' => esc_html__('Profile Button Link', 'calio-addon'),
            'type' => Controls_Manager::URL,
            'default' => ['url' => ''],
            'condition' => ['show_button' => 'yes'],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('socials_section', ['label' => esc_html__('Social Links', 'calio-addon'), 'condition' => ['show_socials' => 'yes']]);
        $repeater = new Repeater();
        $repeater->add_control('icon', [
            'label' => esc_html__('Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fab fa-twitter',
                'library' => 'fa-brands',
            ],
        ]);
        $repeater->add_control('link', [
            'label' => esc_html__('Link', 'calio-addon'),
            'type' => Controls_Manager::URL,
            'placeholder' => 'https://example.com',
            'default' => ['url' => ''],
        ]);

        $this->add_control('social_links', [
            'label' => esc_html__('Social Items', 'calio-addon'),
            'type' => Controls_Manager::REPEATER,
            'fields' => $repeater->get_controls(),
            'title_field' => '{{{ elementor.helpers.renderIcon( this, icon, {}, "i", "panel" ) }}}',
            'default' => [
                [
                    'icon' => [
                        'value' => 'fab fa-twitter',
                        'library' => 'fa-brands',
                    ],
                    'link' => ['url' => ''],
                ],
                [
                    'icon' => [
                        'value' => 'fab fa-linkedin-in',
                        'library' => 'fa-brands',
                    ],
                    'link' => ['url' => ''],
                ],
                [
                    'icon' => [
                        'value' => 'fab fa-dribbble',
                        'library' => 'fa-brands',
                    ],
                    'link' => ['url' => ''],
                ],
            ],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_card', ['label' => esc_html__('Card', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->start_controls_tabs('team_member_card_tabs');
        $this->start_controls_tab('team_member_card_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_background_type_control('team_member_card_bg', '{{WRAPPER}} .calio-team-member');
        $this->add_border_paint_controls('team_member_card_border_color', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member');
        $this->end_controls_tab();
        $this->start_controls_tab('team_member_card_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_background_type_control('team_member_card_bg_hover', '{{WRAPPER}} .calio-team-member:hover');
        $this->add_border_paint_controls('team_member_card_border_color_hover', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member:hover');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_card_border', 'selector' => '{{WRAPPER}} .calio-team-member']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'team_member_card_shadow', 'selector' => '{{WRAPPER}} .calio-team-member']);
        $this->add_dimensions_control('team_member_card_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('team_member_card_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-team-member', 'padding');
        $this->end_controls_section();

        $this->start_controls_section('style_image', ['label' => esc_html__('Image', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_control('team_member_image_height', [
            'label' => esc_html__('Image Height', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 140, 'max' => 520]],
            'default' => ['size' => 320],
            'selectors' => ['{{WRAPPER}} .calio-team-member-media img' => 'height: {{SIZE}}{{UNIT}};'],
        ]);
        $this->add_dimensions_control('team_member_image_radius', esc_html__('Image Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-media, {{WRAPPER}} .calio-team-member-media img', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_image_border', 'selector' => '{{WRAPPER}} .calio-team-member-media']);
        $this->end_controls_section();

        $this->start_controls_section('style_content', ['label' => esc_html__('Content', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_name_typography', 'selector' => '{{WRAPPER}} .calio-team-member-name']);
        $this->add_text_paint_controls('team_member_name_text', esc_html__('Name Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-name');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_role_typography', 'selector' => '{{WRAPPER}} .calio-team-member-role']);
        $this->add_text_paint_controls('team_member_role_text', esc_html__('Role Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-role');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_chip_typography', 'selector' => '{{WRAPPER}} .calio-team-member-chip']);
        $this->add_text_paint_controls('team_member_chip_text', esc_html__('Info Chip Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-chip');
        $this->add_background_type_control('team_member_chip_bg', '{{WRAPPER}} .calio-team-member-chip');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_chip_border', 'selector' => '{{WRAPPER}} .calio-team-member-chip']);
        $this->add_dimensions_control('team_member_chip_radius', esc_html__('Info Chip Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-chip', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_style_heading('team_member_meta_heading', esc_html__('Meta Row', 'calio-addon'));
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_meta_value_typography', 'selector' => '{{WRAPPER}} .calio-team-member-meta-value']);
        $this->add_text_paint_controls('team_member_meta_value_text', esc_html__('Meta Value Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-meta-value');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_meta_label_typography', 'selector' => '{{WRAPPER}} .calio-team-member-meta-label']);
        $this->add_text_paint_controls('team_member_meta_label_text', esc_html__('Meta Label Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-meta-label');
        $this->add_background_type_control('team_member_meta_bg', '{{WRAPPER}} .calio-team-member-meta-row');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_meta_border', 'selector' => '{{WRAPPER}} .calio-team-member-meta-row']);
        $this->add_dimensions_control('team_member_meta_radius', esc_html__('Meta Row Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-meta-row', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('team_member_meta_padding', esc_html__('Meta Row Padding', 'calio-addon'), '{{WRAPPER}} .calio-team-member-meta-row', 'padding');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_bio_typography', 'selector' => '{{WRAPPER}} .calio-team-member-bio']);
        $this->add_text_paint_controls('team_member_bio_text', esc_html__('Bio Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-bio');
        $this->add_style_heading('team_member_contact_heading', esc_html__('Contact Link', 'calio-addon'));
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_contact_typography', 'selector' => '{{WRAPPER}} .calio-team-member-contact-link']);
        $this->add_text_paint_controls('team_member_contact_text', esc_html__('Contact Link Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-contact-link');
        $this->end_controls_section();

        $this->start_controls_section('style_badge', ['label' => esc_html__('Badges', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_floating_label_typography', 'selector' => '{{WRAPPER}} .calio-team-member-floating-label']);
        $this->add_text_paint_controls('team_member_floating_label_text', esc_html__('Floating Label Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-floating-label');
        $this->add_background_type_control('team_member_floating_label_bg', '{{WRAPPER}} .calio-team-member-floating-label');
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'team_member_designation_badge_typography', 'selector' => '{{WRAPPER}} .calio-team-member-designation-badge']);
        $this->add_text_paint_controls('team_member_designation_badge_text', esc_html__('Designation Badge Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-designation-badge');
        $this->add_background_type_control('team_member_designation_badge_bg', '{{WRAPPER}} .calio-team-member-designation-badge');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_designation_badge_border', 'selector' => '{{WRAPPER}} .calio-team-member-designation-badge']);
        $this->add_dimensions_control('team_member_designation_badge_radius', esc_html__('Designation Badge Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-designation-badge', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_polish', ['label' => esc_html__('Polish', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_background_type_control('team_member_ring_bg', '{{WRAPPER}} .calio-team-member-image-ring');
        $this->add_background_type_control('team_member_glow_bg', '{{WRAPPER}} .calio-team-member-panel-glow');
        $this->end_controls_section();

        $this->start_controls_section('style_socials', ['label' => esc_html__('Social Links', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['show_socials' => 'yes']]);
        $this->add_text_paint_controls('team_member_social_text', esc_html__('Icon Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-social-link');
        $this->add_background_type_control('team_member_social_bg', '{{WRAPPER}} .calio-team-member-social-link');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_social_border', 'selector' => '{{WRAPPER}} .calio-team-member-social-link']);
        $this->add_dimensions_control('team_member_social_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-social-link', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_button', ['label' => esc_html__('Button', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['show_button' => 'yes']]);
        $this->add_text_paint_controls('team_member_button_text', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-team-member-button');
        $this->add_background_type_control('team_member_button_bg', '{{WRAPPER}} .calio-team-member-button');
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'team_member_button_border', 'selector' => '{{WRAPPER}} .calio-team-member-button']);
        $this->add_dimensions_control('team_member_button_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-team-member-button', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_dimensions_control('team_member_button_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-team-member-button', 'padding');
        $this->end_controls_section();
    }

    protected function render(): void
    {
        $settings = $this->get_settings_for_display();
        $classes = [
            'calio-team-member',
            'skin-' . sanitize_html_class((string) ($settings['skin'] ?? 'simple')),
            'shape-' . sanitize_html_class((string) ($settings['image_shape'] ?? 'rounded')),
            'hover-' . sanitize_html_class((string) ($settings['image_hover_skin'] ?? 'zoom')),
            'motion-' . sanitize_html_class((string) ($settings['card_motion'] ?? 'soft')),
            'reveal-' . sanitize_html_class((string) ($settings['social_reveal_motion'] ?? 'slide')),
        ];
        if (($settings['show_image_ring'] ?? '') === 'yes') {
            $classes[] = 'has-image-ring';
        }
        if (($settings['show_panel_glow'] ?? '') === 'yes') {
            $classes[] = 'has-panel-glow';
        }

        $name_tag = (string) ($settings['name_tag'] ?? 'h3');
        if (!in_array($name_tag, ['h1', 'h2', 'h3', 'h4', 'div', 'span', 'p'], true)) {
            $name_tag = 'h3';
        }

        echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';
        if (($settings['show_panel_glow'] ?? '') === 'yes') {
            echo '<span class="calio-team-member-panel-glow" aria-hidden="true"></span>';
        }
        if (!empty($settings['image']['url'])) {
            $url = (string) $settings['image']['url'];
            $alt = get_post_meta((int) ($settings['image']['id'] ?? 0), '_wp_attachment_image_alt', true);
            echo '<div class="calio-team-member-media">';
            if (($settings['show_image_ring'] ?? '') === 'yes') {
                echo '<span class="calio-team-member-image-ring" aria-hidden="true"></span>';
            }
            echo '<img src="' . esc_url($url) . '" alt="' . esc_attr($alt ?: wp_basename($url)) . '">';
            if (!empty($settings['floating_label'])) {
                echo '<span class="calio-team-member-floating-label">' . esc_html((string) $settings['floating_label']) . '</span>';
            }
            if (!empty($settings['designation_badge'])) {
                echo '<span class="calio-team-member-designation-badge">' . esc_html((string) $settings['designation_badge']) . '</span>';
            }
            echo '</div>';
        }

        echo '<div class="calio-team-member-content">';
        if (!empty($settings['name'])) {
            echo '<' . tag_escape($name_tag) . ' class="calio-team-member-name">' . esc_html((string) $settings['name']) . '</' . tag_escape($name_tag) . '>';
        }
        if (!empty($settings['role'])) {
            echo '<div class="calio-team-member-role">' . esc_html((string) $settings['role']) . '</div>';
        }
        if (!empty($settings['info_chip'])) {
            echo '<div class="calio-team-member-chip">' . esc_html((string) $settings['info_chip']) . '</div>';
        }
        if (($settings['show_meta_row'] ?? 'yes') === 'yes' && (!empty($settings['meta_value']) || !empty($settings['meta_label']))) {
            echo '<div class="calio-team-member-meta-row">';
            if (!empty($settings['meta_value'])) {
                echo '<span class="calio-team-member-meta-value">' . esc_html((string) $settings['meta_value']) . '</span>';
            }
            if (!empty($settings['meta_label'])) {
                echo '<span class="calio-team-member-meta-label">' . esc_html((string) $settings['meta_label']) . '</span>';
            }
            echo '</div>';
        }
        if (!empty($settings['bio'])) {
            echo '<div class="calio-team-member-bio">' . esc_html((string) $settings['bio']) . '</div>';
        }

        $socials = isset($settings['social_links']) && is_array($settings['social_links']) ? $settings['social_links'] : [];
        if (($settings['show_socials'] ?? 'yes') === 'yes' && $socials !== []) {
            $social_markup = '';
            foreach ($socials as $social) {
                $link = $this->build_link_attributes($social['link'] ?? []);
                if ($link['url'] === '') {
                    continue;
                }
                $social_markup .= '<a class="calio-team-member-social-link" href="' . esc_url($link['url']) . '"';
                if ($link['target'] !== '') {
                    $social_markup .= ' target="' . esc_attr($link['target']) . '"';
                }
                if ($link['rel'] !== '') {
                    $social_markup .= ' rel="' . esc_attr($link['rel']) . '"';
                }
                $social_markup .= '>';
                $social_markup .= wp_kses_post($this->get_icon_markup($social['icon'] ?? []));
                $social_markup .= '</a>';
            }

            if ($social_markup !== '') {
                echo '<div class="calio-team-member-socials">' . wp_kses_post($social_markup) . '</div>';
            }
        }

        if (($settings['show_contact_link'] ?? 'yes') === 'yes' && !empty($settings['contact_link_text']) && !empty($settings['contact_link_url']['url'])) {
            $link = $this->build_link_attributes($settings['contact_link_url']);
            echo '<a class="calio-team-member-contact-link" href="' . esc_url($link['url']) . '"';
            if ($link['target'] !== '') {
                echo ' target="' . esc_attr($link['target']) . '"';
            }
            if ($link['rel'] !== '') {
                echo ' rel="' . esc_attr($link['rel']) . '"';
            }
            echo '>' . esc_html((string) $settings['contact_link_text']) . '</a>';
        }

        if (($settings['show_button'] ?? 'yes') === 'yes' && !empty($settings['profile_button_text']) && !empty($settings['profile_button_url']['url'])) {
            $link = $this->build_link_attributes($settings['profile_button_url']);
            echo '<a class="calio-team-member-button" href="' . esc_url($link['url']) . '"';
            if ($link['target'] !== '') {
                echo ' target="' . esc_attr($link['target']) . '"';
            }
            if ($link['rel'] !== '') {
                echo ' rel="' . esc_attr($link['rel']) . '"';
            }
            echo '>' . esc_html((string) $settings['profile_button_text']) . '</a>';
        }

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

    /**
     * Builds normalized link attributes for social and button output.
     *
     * @param array<string, mixed> $link
     * @return array{url:string,target:string,rel:string}
     */
    private function build_link_attributes(array $link): array
    {
        $href = isset($link['url']) ? (string) $link['url'] : '';
        $target = !empty($link['is_external']) ? '_blank' : '';
        $rel = [];

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

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

        return [
            'url' => $href,
            'target' => $target,
            'rel' => implode(' ', array_unique($rel)),
        ];
    }

}
