<?php
/**
 * No Agents Notice Class
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

use SAAI\Helpers\Connect;
use SAAI\Helpers\Helper;
use SAAI\Factories\Vite_App;
use SAAI\Traits\Singleton_Trait;

/**
 * No_Agents_Notice Class
 *
 * Handles the admin notice when no AI agents are enabled but the store is connected.
 *
 * @since 1.0.0
 */
class No_Agents_Notice {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @var No_Agents_Notice $instance object
     */
    protected static $instance;

    /**
     * The action scheduler hook name for reshowing the notice
     */
    const RESHOW_NOTICE_HOOK = 'saai_reshow_no_agents_notice';

    /**
     * The transient name for storing the notice dismissal
     */
    const NOTICE_DISMISSED_TRANSIENT = 'saai_no_agents_notice_dismissed';

    /**
     * Check if any AI agents are enabled
     *
     * @return bool
     */
    protected function has_enabled_agents() {
        $ai_objects = require SAAI_PLUGIN_DIR_PATH . 'bootstrap/ai-objects.php';

        foreach ( $ai_objects as $agent ) {
            if ( $agent->is_enabled() ) {
                return true;
            }
        }

        return false;
    }

    /**
     * Enable all AI agents
     *
     * @return void
     */
    public function enable_all_agents() {
        check_ajax_referer( 'saai-enable-agents', 'nonce' );

        if ( ! current_user_can( 'manage_woocommerce' ) ) {
            wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'storeagent-ai-for-woocommerce' ) ) );
        }

        $ai_objects    = require SAAI_PLUGIN_DIR_PATH . 'bootstrap/ai-objects.php';
        $enabled_count = 0;

        foreach ( $ai_objects as $agent ) {
            if ( ! $agent->is_enabled() && $agent->enable() ) {
                ++$enabled_count;
            }
        }

        delete_transient( self::NOTICE_DISMISSED_TRANSIENT );

        wp_send_json_success(
            array(
                'message' => sprintf(
                    /* translators: %d: number of agents enabled */
                    _n(
                        'Thanks! %d StoreAgent AI tool has been enabled.',
                        'Thanks! %d StoreAgent AI tools have been enabled.',
                        $enabled_count,
                        'storeagent-ai-for-woocommerce'
                    ),
                    $enabled_count
                ),
            )
        );
    }

    /**
     * Check if we should show the notice
     *
     * @return bool
     */
    protected function should_show_notice() {
        // Only show on admin pages.
        if ( ! is_admin() ) {
            return false;
        }

        // Check if notice is currently dismissed.
        if ( get_transient( self::NOTICE_DISMISSED_TRANSIENT ) ) {
            return false;
        }

        // Only show if store is connected but no agents are enabled.
        return Connect::is_connected() && ! $this->has_enabled_agents();
    }

    /**
     * Display the admin notice
     *
     * @return void
     */
    public function display_notice() {
        if ( ! $this->should_show_notice() ) {
            return;
        }

        ?>
        <div class="notice notice-info is-dismissible" data-notice-id="saai-no-agents">
            <div style="display: flex; align-items: center; padding: 8px 0;">
                <p style="margin: 0 5px 0 0;">
                    <?php esc_html_e( "You don't currently have any StoreAgent tools active, would you like to turn on the defaults?", 'storeagent-ai-for-woocommerce' ); ?>
                </p>
                <button type="button" class="button button-primary" id="saai-enable-all-agents">
                    <?php esc_html_e( 'Turn On All Defaults', 'storeagent-ai-for-woocommerce' ); ?>
                </button>
            </div>
        </div>
        <?php
    }

    /**
     * Handle the notice dismissal
     *
     * @return void
     */
    public function handle_notice_dismissal() {
        check_ajax_referer( 'saai-dismiss-notice', 'nonce' );

        // Set transient for 1 week.
        set_transient( self::NOTICE_DISMISSED_TRANSIENT, true, WEEK_IN_SECONDS );

        // Schedule action to reshow notice after 1 week.
        if ( ! as_next_scheduled_action( self::RESHOW_NOTICE_HOOK ) ) {
            as_schedule_single_action( time() + WEEK_IN_SECONDS, self::RESHOW_NOTICE_HOOK );
        }

        wp_send_json_success();
    }

    /**
     * Handle the reshow notice action
     *
     * @return void
     */
    public function handle_reshow_notice() {
        delete_transient( self::NOTICE_DISMISSED_TRANSIENT );
    }

    /**
     * Enqueue scripts and styles for the notice
     *
     * @return void
     */
    public function enqueue_scripts() {
        if ( $this->should_show_notice() ) {
            $script = new Vite_App(
                'saai-no-agents-notice',
                'src/vanilla/notice/no-agents-notice.ts',
                array( 'jquery' ),
                array(
                    'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
                    'nonce'        => wp_create_nonce( 'saai-enable-agents' ),
                    'dismissNonce' => wp_create_nonce( 'saai-dismiss-notice' ),
                ),
                'saaiNoAgentsNotice'
            );
            $script->enqueue();
        }
    }

    /**
     * Add hooks and filters
     *
     * @return void
     */
    public function run() {
        add_action( 'admin_notices', array( $this, 'display_notice' ) );
        add_action( 'wp_ajax_saai_dismiss_no_agents_notice', array( $this, 'handle_notice_dismissal' ) );
        add_action( 'wp_ajax_saai_enable_all_agents', array( $this, 'enable_all_agents' ) );
        add_action( self::RESHOW_NOTICE_HOOK, array( $this, 'handle_reshow_notice' ) );
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    }
}
