<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class JS_Search_Integration {
/*
    public function __construct() {
        // Skripte registrieren
        add_action('wp_enqueue_scripts', [$this, 'enqueue_assets']);
        
        // Shortcode registrieren
        add_shortcode('JS_Suche_Widget', [$this, 'render_search_form']);
        
        // Widget registrieren
        add_action('widgets_init', [$this, 'register_search_widget']);
    }
*/
    public function enqueue_assets() {
        // Relativer Pfad zum Plugin-Verzeichnis (angepasst für Integration)
        $plugin_url = plugin_dir_url(__FILE__);
        $rel_path = str_replace(home_url(), '', $plugin_url);

        wp_enqueue_style('go-suche-style', $plugin_url . 'style.css');
        wp_enqueue_script('go-suche-script', $plugin_url . 'search.js', array(), '1.0', true);

        wp_localize_script('go-suche-script', 'searchSettings', array(
            'indexUrl' => '/wp/index.json' 
        ));
    }

    public function render_search_form() {
        return '
        <div class="suche-container">
            <input type="text" id="search-input" placeholder="Suchbegriff eingeben..." autocomplete="off">
            <div id="search-results" class="search-results-dropdown"></div>
        </div>';
    }

    public function register_search_widget() {
        register_widget('JS_Search_Widget_Class');
    }
}

/**
 * Die Widget-Klasse bleibt separat, wird aber über die Hauptklasse aufgerufen.
 */
class JS_Search_Widget_Class extends WP_Widget {
    public function __construct() {
        parent::__construct(
            'js_search_widget', 
            'JS-Suchfeld (Integriert)', 
            array('description' => 'Suchfeld für index.json')
        );
    }

    public function widget($args, $instance) {
        echo $args['before_widget'];
        if (!empty($instance['title'])) {
            echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
        }
        // Zugriff auf die Form-Methode
        $search_tool = new JS_Search_Integration();
        echo $search_tool->render_search_form();
        echo $args['after_widget'];
    }

    public function form($instance) {
        $title = !empty($instance['title']) ? $instance['title'] : '';
        echo '<p><label>Titel:</label><input class="widefat" name="'.$this->get_field_name('title').'" type="text" value="'.esc_attr($title).'"></p>';
    }

    public function update($new_instance, $old_instance) {
        return array('title' => (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '');
    }
}
