<?php
/**
 * AI Memory Vector Availability admin notice template.
 *
 * @package SAAI
 */

if ( ! defined( 'ABSPATH' ) ) {
    die( 'You are not allowed to call this page directly.' );
}

?>

<div class="notice notice-info is-dismissible saai-vector-availability-notice" style="display: flex; align-items: center; gap: 10px; padding: 10px; flex-wrap: wrap;">
    <img src="<?php echo esc_url( SAAI_PLUGIN_URL . '/static/images/storeagent-icon.png' ); ?>" alt="StoreAgent Icon"  style="width: 80px; height: 80px;">
    <div class="storeagent-vector-availability-notice-content" style="flex: 1;">
        <p>
            <strong><?php esc_html_e( 'StoreAgent AI Memory', 'storeagent-ai-for-woocommerce' ); ?></strong>
        </p>
        <p>
            <?php
            if ( $posts_to_show === $posts_count ) {
                printf(
                    /* translators: %d: number of posts */
                    esc_html(
                        /* translators: %d: number of posts */
                        _n(
                            'You have %d post that hasn\'t been uploaded to the AI memory yet.',
                            'You have %d posts that haven\'t been uploaded to the AI memory yet.',
                            $posts_count,
                            'storeagent-ai-for-woocommerce'
                        )
                    ),
                    esc_html( number_format_i18n( $posts_count ) )
                );
            } else {
                printf(
                    /* translators: %1$d: number of posts that can be uploaded, %2$d: total number of posts */
                    esc_html__( 'You can upload %1$d more posts to AI memory (out of %2$d posts waiting).', 'storeagent-ai-for-woocommerce' ),
                    esc_html( number_format_i18n( $posts_to_show ) ),
                    esc_html( number_format_i18n( $posts_count ) )
                );
            }
            ?>
            <?php esc_html_e( 'Upload them now to improve your StoreAgent\'s knowledge and customer support capabilities.', 'storeagent-ai-for-woocommerce' ); ?>
        </p>
        <p>
            <button type="button" class="button button-primary" id="saai-schedule-upload-btn">
                <?php esc_html_e( 'Upload Posts to AI Memory', 'storeagent-ai-for-woocommerce' ); ?>
            </button>
        </p>
    </div>
</div>


<script>
    jQuery(document).ready(function($) {
        $('#saai-schedule-upload-btn').on('click', function(e) {
            e.preventDefault();
            var $button = $(this);
            var $notice = $('.saai-vector-availability-notice');
            
            $button.prop('disabled', true);
            
            $.ajax({
                url: '<?php echo esc_url_raw( admin_url( 'admin-ajax.php' ) ); ?>',
                type: 'POST',
                data: {
                    action: 'saai_retry_upload_to_ai_memory',
                    nonce: '<?php echo esc_attr( wp_create_nonce( 'saai_retry_upload_to_ai_memory' ) ); ?>'
                },
                success: function(response) {
                    // Remove the notice on success (whether newly scheduled or already scheduled)
                    // If upload is already scheduled, that's the desired state, so hide the notice
                    $notice.remove();
                },
                error: function(xhr, status, error) {
                    // If AJAX errored, re-enable button
                    $button.prop('disabled', false);
                    console.error('AJAX error:', error);
                }
            });
        });
    });
</script>
