<?php
namespace AI_BotKit\Integration;

/**
 * PEP (Product Enquiry for WooCommerce) detection utility.
 *
 * @package AI_BotKit\Integration
 */
class PEP_Detector {

    /**
     * Check if PEP plugin is active.
     *
     * @return bool
     */
    public static function is_pep_active(): bool {
        if ( ! function_exists( 'is_plugin_active' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        return is_plugin_active( 'product-enquiry-for-woocommerce/product-enquiry-for-woocommerce.php' ) || is_plugin_active('product-enquiry-pro/product-enquiry-pro.php');
    }

    /**
     * Check if WooCommerce is active.
     *
     * @return bool
     */
    public static function is_woocommerce_active(): bool {
        return class_exists( 'WooCommerce' );
    }

    /**
     * Register admin notice when PEP is inactive.
     *
     * @return void
     */
    public static function require_pep(): void {
        if ( ! self::is_pep_active() && self::is_woocommerce_active() ) {
            add_action( 'admin_notices', [ self::class, 'render_pep_notice' ] );
        }
    }

    /**
     * Render the admin notice for missing PEP.
     *
     * @return void
     */
    public static function render_pep_notice(): void {
        $screen = get_current_screen();
        if ( ! $screen || strpos( $screen->id, 'ai-botkit' ) === false ) {
            return;
        }

        echo '<div class="notice notice-warning is-dismissible">';
        echo '<p>' . sprintf(
            /* translators: 1: Opening anchor tag, 2: Closing anchor tag */
            esc_html__( 'PEP (Product Enquiry for WooCommerce) is not active. %1$sInstall/Activate PEP%2$s to use AI BotKit integration features.', 'ai-botkit-for-lead-generation' ),
            '<a href="https://wordpress.org/plugins/product-enquiry-for-woocommerce/" target="_blank">',
            '</a>'
        ) . '</p>';
        echo '</div>';
    }
}
