<?php
/**
 * Author: Rymera Web Co
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

use SAAI\Helpers\Connect;
use SAAI\Factories\Admin_Notice;
use SAAI\Traits\Singleton_Trait;

/**
 * Class Connect_Notice
 *
 * Handles the admin notice for unconnected stores.
 *
 * @since 1.0.0
 */
class Connect_Notice {

    use Singleton_Trait;

    /**
     * Holds the class instance object
     *
     * @since 1.0.0
     * @var Connect_Notice $instance
     */
    protected static $instance;

    /**
     * Option name for storing the notice dismissal timestamp.
     *
     * @since 1.0.0
     */
    public const DISMISS_OPTION = 'storeagent_connect_notice_dismissed';

    /**
     * Run the class functionality.
     *
     * @since 1.0.0
     * @return void
     */
    public function run() {
        add_action( 'admin_notices', array( $this, 'maybe_show_notice' ) );
        add_action( 'wp_ajax_storeagent_dismiss_connect_notice', array( $this, 'ajax_dismiss_notice' ) );

        // Only add script if we need to show the notice.
        if ( $this->should_show_notice() ) {
            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
        }
    }

    /**
     * Check if we should show the notice.
     *
     * @since 1.0.0
     * @return bool
     */
    protected function should_show_notice() {
        // Only show to admins.
        if ( ! current_user_can( 'manage_options' ) ) {
            return false;
        }

        // Don't show if already connected.
        if ( Connect::is_connected() ) {
            return false;
        }

        // Check if notice was dismissed in the last 24 hours.
        $dismissed_time = get_option( self::DISMISS_OPTION, 0 );
        if ( $dismissed_time && ( time() - $dismissed_time ) < DAY_IN_SECONDS ) {
            return false;
        }

        // Don't show on the plugin pages.
        if ( isset( $_GET['page'] ) && in_array( // phpcs:ignore
            $_GET['page'], // phpcs:ignore
            array(
                Admin_Menu_Handler::PARENT_SLUG,
                Admin_Menu_Handler::LEGACY_PARENT_SLUG,
                Admin_Menu_Handler::SETTINGS_SLUG,
                Admin_Menu_Handler::AGENTS_SLUG,
            ),
            true
        ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
            return false;
        }

        return true;
    }

    /**
     * Enqueue the necessary scripts.
     *
     * @since 1.0.0
     * @return void
     */
    public function enqueue_scripts() {
        wp_register_script(
            'storeagent-connect-notice',
            plugins_url( 'static/js/connect-notice.js', SAAI_PLUGIN_FILE ),
            array( 'jquery' ),
            filemtime( SAAI_PLUGIN_DIR_PATH . 'static/js/connect-notice.js' ),
            true
        );

        wp_localize_script(
            'storeagent-connect-notice',
            'storeagentConnectNotice',
            array(
                'ajaxUrl' => admin_url( 'admin-ajax.php' ),
                'nonce'   => wp_create_nonce( 'storeagent_dismiss_connect_notice' ),
            )
        );

        wp_enqueue_script( 'storeagent-connect-notice' );
    }

    /**
     * Handle AJAX notice dismissal.
     *
     * @since 1.0.0
     * @return void
     */
    public function ajax_dismiss_notice() {
        check_ajax_referer( 'storeagent_dismiss_connect_notice', 'nonce' );

        if ( ! current_user_can( 'manage_options' ) ) {
            wp_die( -1 );
        }

        update_option( self::DISMISS_OPTION, time() );
        wp_die( 1 );
    }

    /**
     * Maybe show the admin notice.
     *
     * @since 1.0.0
     * @return void
     */
    public function maybe_show_notice() {
        if ( ! $this->should_show_notice() ) {
            return;
        }

        $connect_url = admin_url( 'admin.php?page=' . Admin_Menu_Handler::PARENT_SLUG );

        $notice = new Admin_Notice(
            sprintf(
                '<p class="storeagent-connect-notice">%s <a href="%s">%s</a></p>',
                esc_html__( 'You need to connect your store to your StoreAgent account.', 'storeagent-ai-for-woocommerce' ),
                esc_url( $connect_url ),
                esc_html__( 'Connect your store', 'storeagent-ai-for-woocommerce' )
            ),
            'info',
            'html'
        );
        $notice->run();
    }
}
