<?php
namespace Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Tags\Post_Tags;

use Elementor\Controls_Manager;
use Elementor\Core\DynamicTags\Tag;
use Elementor\Modules\DynamicTags\Module;
use Softech_Addons_for_Elementor\Includes\Dynamic_Tags\Groups\Softech_Post_Group;

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

/**
 * Partner Taxonomy Term URL – Dynamic Tag
 *
 * Outputs get_term_link( $term ) for the current term being rendered
 * in an Elementor loop — exactly like your shortcode does with foreach($terms).
 *
 * Resolves the current term from (in priority order):
 *   1. $wp_query->loop_term  — set by Elementor Pro's taxonomy loop provider
 *   2. get_queried_object()  — on a term archive page
 *   3. global $wp_query->queried_object — fallback
 *
 * No ACF field needed. No post ID needed.
 * Just drop this file in includes/dynamic-tags/tags/post-tags/
 * and use it as the link URL on your loop container.
 */
class Softech_Partner_Term_Url extends Tag {

    public function get_name() {
        return 'softech-partner-term-url';
    }

    public function get_title() {
        return esc_html__( 'Partner Term URL', 'softech-addons-for-elementor' );
    }

    public function get_group() {
        return Softech_Post_Group::get_name();
    }

    public function get_categories() {
        return [ Module::URL_CATEGORY ];
    }

    protected function register_controls() {

        // Taxonomy selector — defaults to 'partner' but works for any taxonomy
        $this->add_control(
            'taxonomy',
            [
                'label'   => esc_html__( 'Taxonomy', 'softech-addons-for-elementor' ),
                'type'    => Controls_Manager::SELECT,
                'options' => $this->get_taxonomy_options(),
                'default' => 'partner',
                'dynamic' => [ 'active' => false ],
            ]
        );

        $this->add_control(
            'fallback',
            [
                'label'       => esc_html__( 'Fallback URL', 'softech-addons-for-elementor' ),
                'type'        => Controls_Manager::TEXT,
                'placeholder' => 'https://',
                'dynamic'     => [ 'active' => false ],
            ]
        );
    }

    public function render() {
        $term = $this->get_current_term();

        if ( ! $term || is_wp_error( $term ) ) {
            echo esc_url( $this->get_settings( 'fallback' ) );
            return;
        }

        $url = get_term_link( $term );

        if ( is_wp_error( $url ) || empty( $url ) ) {
            echo esc_url( $this->get_settings( 'fallback' ) );
            return;
        }

        echo esc_url( $url );
    }

    /**
     * Resolve the current WP_Term being rendered in the loop.
     *
     * Mirrors exactly how Elementor Pro's Dynamic_Value_Provider resolves
     * the term in get_taxonomy_field_data():
     *
     *   global $wp_query;
     *   $field = $this->get_field_object( $field_key, $wp_query->loop_term );
     *
     * $wp_query->loop_term is set by Elementor Pro's Taxonomy_Loop_Provider
     * on each iteration of a taxonomy loop. Outside that context we fall back
     * to get_queried_object() which works on standard term archive pages.
     *
     * @return WP_Term|null
     */
    private function get_current_term() {
        global $wp_query;

        // 1. Elementor Pro taxonomy loop — loop_term is a WP_Term object
        if ( ! empty( $wp_query->loop_term ) && $wp_query->loop_term instanceof \WP_Term ) {
            return $wp_query->loop_term;
        }

        // 2. Standard term archive page
        $queried = get_queried_object();
        if ( $queried instanceof \WP_Term ) {
            return $queried;
        }

        // 3. wp_query->queried_object fallback
        if ( ! empty( $wp_query->queried_object ) && $wp_query->queried_object instanceof \WP_Term ) {
            return $wp_query->queried_object;
        }

        return null;
    }

    /**
     * Build taxonomy dropdown options.
     *
     * @return array<string, string>
     */
    private function get_taxonomy_options(): array {
        $options    = [];
        $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' );

        foreach ( $taxonomies as $tax ) {
            $options[ $tax->name ] = $tax->label;
        }

        return $options;
    }
}