<?php
namespace NewsletterBridge\Admin\Tabs\Newsletter;

/**
 * Gestione autocompletamento per NewsletterTab
 * Responsabilità: AJAX autocompletamento per tassonomie e post types
 */
class NewsletterAutocomplete {
    
    public static function init() {
        add_action('wp_ajax_get_taxonomies_autocomplete', [__CLASS__, 'get_taxonomies_autocomplete']);
        add_action('wp_ajax_get_post_types_autocomplete', [__CLASS__, 'get_post_types_autocomplete']);
    }
    
    public static function get_taxonomies_autocomplete() {
        check_ajax_referer('newsletter_autocomplete', 'nonce');
        
        $search = sanitize_text_field($_POST['search'] ?? '');
        $taxonomy = sanitize_text_field($_POST['taxonomy'] ?? 'post_tag');
        
        // Tassonomie di servizio da escludere
        $excluded_taxonomies = ['attachment_category', 'attachment_tag', 'nav_menu', 'link_category', 'wp_theme', 'wp_template_part_area'];
        
        // Se non è specificata una tassonomia, cerca in tutte le tassonomie pubbliche
        if ($taxonomy === 'all') {
            $taxonomies = get_taxonomies(['public' => true], 'objects');
            $results = [];
            
            foreach ($taxonomies as $tax) {
                if (in_array($tax->name, $excluded_taxonomies)) {
                    continue;
                }
                
                $terms = get_terms([
                    'taxonomy' => $tax->name,
                    'search' => $search,
                    'number' => 10,
                    'hide_empty' => false
                ]);
                
                foreach ($terms as $term) {
                    $results[] = [
                        'id' => $term->slug,
                        'text' => $tax->label . ' -> ' . $term->name,
                        'slug' => $term->slug,
                        'taxonomy' => $tax->name
                    ];
                }
            }
        } else {
            // Ricerca in una tassonomia specifica
            $terms = get_terms([
                'taxonomy' => $taxonomy,
                'search' => $search,
                'number' => 10,
                'hide_empty' => false
            ]);
            
            $results = [];
            foreach ($terms as $term) {
                $results[] = [
                    'id' => $term->slug,
                    'text' => $term->name,
                    'slug' => $term->slug,
                    'taxonomy' => $taxonomy
                ];
            }
        }
        
        wp_send_json($results);
    }
    
    public static function get_post_types_autocomplete() {
        check_ajax_referer('newsletter_autocomplete', 'nonce');
        
        $search = sanitize_text_field($_POST['search'] ?? '');
        
        $post_types = get_post_types(['public' => true], 'objects');
        $results = [];
        
        foreach ($post_types as $post_type) {
            if (stripos($post_type->name, $search) !== false || stripos($post_type->label, $search) !== false) {
                $results[] = [
                    'id' => $post_type->name,
                    'text' => $post_type->label . ' (' . $post_type->name . ')',
                    'name' => $post_type->name
                ];
            }
        }
        
        wp_send_json($results);
    }
}
