<?php

namespace Calio\Extensions;

use Elementor\Controls_Manager;

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

/**
 * Turns Elementor elements into safe clickable wrappers without hijacking nested interactive controls.
 */
class WrapperLinkExtension
{
    public function __construct()
    {
        add_action('elementor/element/common/section_effects/after_section_end', [$this, 'register_controls']);
        add_action('elementor/element/section/section_effects/after_section_end', [$this, 'register_controls']);
        add_action('elementor/element/column/section_effects/after_section_end', [$this, 'register_controls']);
        add_action('elementor/element/container/section_effects/after_section_end', [$this, 'register_controls']);

        add_action('elementor/frontend/widget/before_render', [$this, 'before_render']);
        add_action('elementor/frontend/section/before_render', [$this, 'before_render']);
        add_action('elementor/frontend/column/before_render', [$this, 'before_render']);
        add_action('elementor/frontend/container/before_render', [$this, 'before_render']);
    }

    /**
     * Registers wrapper link controls in the Advanced tab.
     *
     * @param object $element Elementor element instance.
     */
    public function register_controls($element): void
    {
        $element->start_controls_section(
            'calio_wrapper_link',
            [
                'label' => esc_html__('Calio Wrapper Link', 'calio-addon'),
                'tab' => Controls_Manager::TAB_ADVANCED,
            ]
        );

        $element->add_control(
            'calio_wrapper_link_enable',
            [
                'label' => esc_html__('Enable Wrapper Link', 'calio-addon'),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default' => '',
            ]
        );

        $element->add_control(
            'calio_wrapper_link_url',
            [
                'label' => esc_html__('Link', 'calio-addon'),
                'type' => Controls_Manager::URL,
                'placeholder' => 'https://example.com',
                'default' => ['url' => ''],
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_label',
            [
                'label' => esc_html__('Accessible Label', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Open linked section', 'calio-addon'),
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_hover',
            [
                'label' => esc_html__('Hover Lift', 'calio-addon'),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default' => 'yes',
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_skin',
            [
                'label' => esc_html__('Hover Skin', 'calio-addon'),
                'type' => Controls_Manager::SELECT,
                'default' => 'soft',
                'options' => [
                    'soft' => esc_html__('Soft', 'calio-addon'),
                    'outline' => esc_html__('Outline', 'calio-addon'),
                    'glow' => esc_html__('Glow', 'calio-addon'),
                ],
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_show_indicator',
            [
                'label' => esc_html__('Show Link Indicator', 'calio-addon'),
                'type' => Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default' => 'yes',
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_indicator_text',
            [
                'label' => esc_html__('Indicator Text', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Open', 'calio-addon'),
                'condition' => [
                    'calio_wrapper_link_enable' => 'yes',
                    'calio_wrapper_link_show_indicator' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'calio_wrapper_link_exclude_selector',
            [
                'label' => esc_html__('Exclude Selector', 'calio-addon'),
                'type' => Controls_Manager::TEXT,
                'default' => '',
                'placeholder' => '.ignore-wrapper-link, .swiper-button',
                'description' => esc_html__('Optional selector for nested areas that should not trigger the wrapper link.', 'calio-addon'),
                'condition' => ['calio_wrapper_link_enable' => 'yes'],
            ]
        );

        $element->end_controls_section();
    }

    /**
     * Adds runtime metadata for the wrapper-link frontend behavior.
     *
     * @param object $element Elementor element instance.
     */
    public function before_render($element): void
    {
        $settings = $element->get_settings_for_display();
        if (($settings['calio_wrapper_link_enable'] ?? '') !== 'yes') {
            return;
        }

        $link = is_array($settings['calio_wrapper_link_url'] ?? null) ? $settings['calio_wrapper_link_url'] : ['url' => ''];
        $url = esc_url_raw(trim((string) ($link['url'] ?? '')));
        if ($url === '') {
            return;
        }

        $skin = $this->allowlisted_value((string) ($settings['calio_wrapper_link_skin'] ?? 'soft'), ['soft', 'outline', 'glow'], 'soft');
        $indicator_text = sanitize_text_field((string) ($settings['calio_wrapper_link_indicator_text'] ?? esc_html__('Open', 'calio-addon')));
        $exclude_selector = sanitize_text_field((string) ($settings['calio_wrapper_link_exclude_selector'] ?? ''));
        $label = sanitize_text_field((string) ($settings['calio_wrapper_link_label'] ?? esc_html__('Open linked section', 'calio-addon')));

        $element->add_render_attribute(
            '_wrapper',
            [
                'data-calio-wrapper-link' => 'yes',
                'data-calio-wrapper-link-url' => $url,
                'data-calio-wrapper-link-external' => !empty($link['is_external']) ? 'yes' : 'no',
                'data-calio-wrapper-link-nofollow' => !empty($link['nofollow']) ? 'yes' : 'no',
                'data-calio-wrapper-link-hover' => (($settings['calio_wrapper_link_hover'] ?? 'yes') === 'yes') ? 'yes' : 'no',
                'data-calio-wrapper-link-skin' => $skin,
                'data-calio-wrapper-link-indicator' => (($settings['calio_wrapper_link_show_indicator'] ?? 'yes') === 'yes') ? 'yes' : 'no',
                'data-calio-wrapper-link-indicator-text' => $indicator_text,
                'data-calio-wrapper-link-exclude-selector' => $exclude_selector,
                'aria-label' => $label,
            ]
        );
    }

    /**
     * Keeps data attributes aligned with declared Elementor select options.
     *
     * @param string   $value    Submitted control value.
     * @param string[] $allowed  Allowed values.
     * @param string   $fallback Fallback value.
     */
    private function allowlisted_value(string $value, array $allowed, string $fallback): string
    {
        $value = sanitize_key($value);

        return in_array($value, $allowed, true) ? $value : $fallback;
    }
}
