<?php

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

class AIEO_DMM_Tag_Extras extends AIEO_DMM_Module_Base {

    public function get_id()       { return 'tag_extras'; }
    public function get_label()    { return __('Tag Extras', 'ai-eshop-optimizer'); }
    public function get_priority() { return 78; }
    public function get_group()    { return 'visuals'; }

    public function register_hooks() {
        if (!is_admin()) { return; }
        $tax = $this->taxonomy();
        add_action($tax . '_edit_form_fields', array($this, 'render_related_panel'), 5, 2);
    }

    private function taxonomy() {
        return (string) apply_filters('aieo_dmm_tag_extras_taxonomy', 'product_tag');
    }

    public function render_related_panel($term, $taxonomy) {
        if (!is_object($term) || !isset($term->term_id))                              { return; }
        if ((string) $taxonomy !== $this->taxonomy())                                  { return; }
        if (!class_exists('AIEO_DMM_Related_Tab'))                                     { return; }

        ?>
        <tr class="form-field aieo-ce-panel-row">
            <th colspan="2" style="padding:0;">
                <div class="aieo-ce-panel">
                    <div class="aieo-ce-panel-head">
                        <h2><?php esc_html_e('AIEO Tag Extras — related entities', 'ai-eshop-optimizer'); ?></h2>
                        <p class="description"><?php esc_html_e('Stored bidirectionally in wp_aieo_dmm_term_relations.', 'ai-eshop-optimizer'); ?></p>
                    </div>
                    <div class="aieo-ce-tabpanel is-active">
                        <?php AIEO_DMM_Related_Tab::render($taxonomy, (int) $term->term_id); ?>
                    </div>
                </div>
            </th>
        </tr>
        <?php
    }

    public function render_subtab() {
        echo '<div class="aieo-dmm-section">';
        echo '<h2>' . esc_html__('Tag Extras', 'ai-eshop-optimizer') . '</h2>';
        echo '<p class="description">' . esc_html__('Symmetric Related panel for the product_tag taxonomy. Edit per-tag relationships at WooCommerce → Products → Tags → click a tag.', 'ai-eshop-optimizer') . '</p>';
        echo '</div>';
    }
}
