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

/**
 * GLSE_Compat_Base - Abstract base for all theme integrations.
 *
 * Each theme subclass must implement:
 *   - register_customizer( WP_Customize_Manager $wp_customize ) : void
 *   - maybe_override() : void
 *
 * Shared helpers available to all subclasses:
 *   - render( string $wrapper_class ) : void
 *   - generic_setting_enabled() : bool
 *   - generic_customizer( WP_Customize_Manager $wp_customize, string $theme_slug ) : void
 */
abstract class GLSE_Compat_Base {

    /**
     * Register Customizer and front-end override hooks.
     *
     * @since 1.4.0
     */
    public function __construct() {
        add_action( 'customize_register', [ $this, 'register_customizer' ], 20 );
        add_action( 'wp',                 [ $this, 'maybe_override' ] );
    }

    /**
     * Register theme-specific Customizer settings.
     *
     * @since 1.4.0
     */
    abstract public function register_customizer( WP_Customize_Manager $wp_customize ): void;

    /**
     * Conditionally override the theme search with Glance Live Search.
     *
     * @since 1.4.0
     */
    abstract public function maybe_override(): void;

    /**
     * Signals to GLSE_Public that assets must be loaded on every page.
     * Call this inside maybe_override() after confirming the override is active.
     */
    protected function force_load_assets(): void {
        add_filter( 'glse_force_load_assets', '__return_true' );
    }

    /**
     * Outputs the Glance search bar wrapped in a theme-compatible <div>.
     *
     * @param string $wrapper_class CSS classes for the outer wrapper.
     */
    protected function render( string $wrapper_class ): void {
        printf(
            '<div class="%s" role="search" aria-label="%s">',
            esc_attr( $wrapper_class ),
            esc_attr__( 'Product Search', 'glance-live-search' )
        );
        echo do_shortcode( '[glance_search]' );
        echo '</div>';
    }

    /**
     * Reads the GLSE opt-in Customizer checkbox for a given theme slug.
     * Returns true when the user has enabled the override.
     *
     * @param string $theme_slug WordPress template slug (e.g. 'storefront').
     */
    protected function generic_setting_enabled( string $theme_slug ): bool {
        return (bool) get_theme_mod( 'glse_override_header_search_' . $theme_slug, false );
    }

    /**
     * Registers a generic "Replace header search" checkbox in a dedicated
     * Glance Live Search Customizer section.
     * Used by themes that do not expose their own search-type selector.
     *
     * @param WP_Customize_Manager $wp_customize
     * @param string               $theme_slug    WordPress template slug.
     * @param string               $theme_label   Human-readable theme name.
     */
    protected function generic_customizer(
        WP_Customize_Manager $wp_customize,
        string $theme_slug,
        string $theme_label
    ): void {
        if ( ! $wp_customize->get_section( 'glse_theme_integration' ) ) {
            $wp_customize->add_section( 'glse_theme_integration', [
                'title'    => __( 'Glance Live Search', 'glance-live-search' ),
                'priority' => 200,
            ] );
        }

        $setting_id = 'glse_override_header_search_' . $theme_slug;

        $wp_customize->add_setting( $setting_id, [
            'default'           => false,
            'transport'         => 'refresh',
            'sanitize_callback' => 'rest_sanitize_boolean',
        ] );

        $wp_customize->add_control( $setting_id, [
            'label'       => __( 'Replace header search with Glance Live Search', 'glance-live-search' ),
            'description' => sprintf(
                /* translators: %s: theme name */
                __( 'Removes the %s default search and replaces it with Glance Live Search.', 'glance-live-search' ),
                $theme_label
            ),
            'section'     => 'glse_theme_integration',
            'type'        => 'checkbox',
        ] );
    }
}
