<?php
/**
 * Disclosure Manager
 *
 * Handles frontend disclosure banners for AI chatbots and tools.
 */

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

class Tiriri_Disclosure {

    /**
     * Initialize disclosure hooks
     */
    public function init() {
        // Only on frontend
        if ( is_admin() ) {
            return;
        }

        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
        add_action( 'wp_footer', array( $this, 'render_chatbot_disclosure' ) );
    }

    /**
     * Enqueue frontend CSS and JS
     */
    public function enqueue_assets() {
        $settings = get_option( 'tiriri_settings', array() );

        $disclosure_enabled = ! empty( $settings['chatbot_disclosure_enabled'] );
        $labeling_enabled   = ! empty( $settings['content_labeling_enabled'] );

        // CSS is needed for both disclosure banners and content labels
        if ( $disclosure_enabled || $labeling_enabled ) {
            wp_enqueue_style(
                'tiriri-disclosure',
                TIRIRI_PLUGIN_URL . 'public/assets/disclosure.css',
                array(),
                TIRIRI_VERSION
            );
        }

        // JS is only needed for the disclosure banner
        if ( ! $disclosure_enabled ) {
            return;
        }

        wp_enqueue_script(
            'tiriri-disclosure',
            TIRIRI_PLUGIN_URL . 'public/assets/disclosure.js',
            array(),
            TIRIRI_VERSION,
            true
        );

        // Pass config to JS
        wp_localize_script( 'tiriri-disclosure', 'tiririConfig', array(
            'enabled'        => ! empty( $settings['chatbot_disclosure_enabled'] ),
            'text'           => $settings['chatbot_disclosure_text'] ?? __( 'You are interacting with an AI assistant.', 'tiriri-transparency-for-eu-ai-act' ),
            'position'       => $settings['chatbot_disclosure_position'] ?? 'before_chat',
            'style'          => $settings['chatbot_disclosure_style'] ?? 'banner',
            'dismissible'    => true,
            'cookieDuration' => 24, // hours
            'scriptPatterns' => tiriri_is_pro() ? $this->get_script_detection_config() : array(),
        ) );
    }

    /**
     * Render the chatbot disclosure banner HTML
     */
    public function render_chatbot_disclosure() {
        $settings = get_option( 'tiriri_settings', array() );

        if ( empty( $settings['chatbot_disclosure_enabled'] ) ) {
            return;
        }

        $scan_results = get_option( 'tiriri_scan_results', array() );
        $has_chatbot = false;

        // Check if any detected AI tools are chatbots (plugins + manual declarations)
        $all_tools = array_merge(
            ! empty( $scan_results['plugins'] ) ? $scan_results['plugins'] : array(),
            ! empty( $scan_results['manual'] ) ? $scan_results['manual'] : array()
        );
        foreach ( $all_tools as $tool ) {
            if ( 'chatbot' === $tool['category'] ) {
                $has_chatbot = true;
                break;
            }
        }

        // Even if no chatbot plugin detected, the JS detector might find third-party scripts
        // So we always render the container, JS will control visibility
        $text  = $settings['chatbot_disclosure_text'] ?? __( 'You are interacting with an AI assistant.', 'tiriri-transparency-for-eu-ai-act' );
        $style = $settings['chatbot_disclosure_style'] ?? 'banner';

        ?>
        <div id="tiriri-disclosure"
             class="tiriri-disclosure tiriri-disclosure--<?php echo esc_attr( $style ); ?>"
             role="status"
             aria-live="polite"
             aria-label="<?php esc_attr_e( 'AI Disclosure Notice', 'tiriri-transparency-for-eu-ai-act' ); ?>"
             data-chatbot-detected="<?php echo esc_attr( $has_chatbot ? 'true' : 'false' ); ?>"
             style="display:none;">
            <div class="tiriri-disclosure__inner">
                <span class="tiriri-disclosure__icon" aria-hidden="true">
                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M8 1C4.134 1 1 4.134 1 8s3.134 7 7 7 7-3.134 7-7-3.134-7-7-7zm0 12.5A5.506 5.506 0 012.5 8 5.506 5.506 0 018 2.5 5.506 5.506 0 0113.5 8 5.506 5.506 0 018 13.5z" fill="currentColor"/>
                        <path d="M8 4.5a.75.75 0 00-.75.75v3.5a.75.75 0 001.5 0v-3.5A.75.75 0 008 4.5zM8 10.25a.75.75 0 100 1.5.75.75 0 000-1.5z" fill="currentColor"/>
                    </svg>
                </span>
                <span class="tiriri-disclosure__text"><?php echo esc_html( $text ); ?></span>
                <button class="tiriri-disclosure__dismiss"
                        aria-label="<?php esc_attr_e( 'Dismiss AI disclosure notice', 'tiriri-transparency-for-eu-ai-act' ); ?>"
                        type="button">
                    <svg width="12" height="12" viewBox="0 0 12 12" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M9.354 3.354a.5.5 0 00-.708-.708L6 5.293 3.354 2.646a.5.5 0 10-.708.708L5.293 6 2.646 8.646a.5.5 0 00.708.708L6 6.707l2.646 2.647a.5.5 0 00.708-.708L6.707 6l2.647-2.646z" fill="currentColor"/>
                    </svg>
                </button>
            </div>
        </div>
        <?php
    }

    /**
     * Get script detection patterns for JavaScript
     */
    private function get_script_detection_config() {
        $scanner = new Tiriri_Scanner();
        $patterns = $scanner->get_script_patterns();

        // Simplify for frontend: just pattern and name
        $config = array();
        foreach ( $patterns as $p ) {
            $config[] = array(
                'pattern'  => $p['pattern'],
                'name'     => $p['name'],
                'category' => $p['category'],
            );
        }
        return $config;
    }
}
