<?php
/**
 * AI Memory Upload Progress admin notice template.
 *
 * @package SAAI
 */

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

$data_upload_instance = \SAAI\Classes\Memory\Data_Upload::instance();

?>

<div class="notice notice-info storeagent-memory-upload-progress-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-chat-setup-notice-content" style="flex: 1;">
        <h3 style="margin: 0 0 5px;"><?php esc_html_e( 'Setting Up Your AI Chat Assistant', 'storeagent-ai-for-woocommerce' ); ?></h3>
        <p style="margin: 0;"><?php esc_html_e( 'We\'re creating your AI memory and uploading your post type data. This may take a few moments depending on the number of posts in your store.', 'storeagent-ai-for-woocommerce' ); ?></p>
        <p style="margin: 0;"><?php esc_html_e( 'You can continue using StoreAgent --- we\'ll notify you once everything is ready.', 'storeagent-ai-for-woocommerce' ); ?></p>
        
        <div style="margin: 10px 0;">
            <progress value="<?php echo esc_attr( $progress ); ?>" max="100" style="width: 100%; height: 20px;"></progress>
            <div style="display: flex; justify-content: space-between; align-items: center; margin-top: 5px; font-size: 12px; color: #666;">
                <span>
                    <?php
                    echo wp_kses_post(
                        sprintf(
                        // translators: %1$s is the estimated time remaining, %2$s is the number of posts processed, %3$s is the total number of posts.
                            __( '%1$s of %2$s posts processed (%3$s)', 'storeagent-ai-for-woocommerce' ),
                            '<strong>' . $progress_data['processed'] . '</strong>',
                            '<strong>' . $progress_data['total'] . '</strong>',
                            '<strong>' . round( $progress, 1 ) . '%</strong>'
                        )
                    );
                    ?>
                </span>
                <span>
                    <?php
                    echo wp_kses_post(
                        sprintf(
                        // translators: %1$s is the estimated time remaining, %2$s is the number of posts processed, %3$s is the total number of posts.
                            __( 'Estimated time remaining: %1$s', 'storeagent-ai-for-woocommerce' ),
                            '<strong>' . $data_upload_instance->format_duration( $progress_data['time_remaining'] ) . '</strong>'
                        )
                    );
                    ?>
                </span>
            </div>
        </div>
    </div>
    <?php if ( ! $has_scheduled ) : ?>
        <div style="padding: 10px 15px; width: 100%; background: #fef1f1;border: 1px solid #ffc0c0;">
            <p style="margin: 0 0 5px"><?php esc_html_e( 'There was an error during the previous upload. The process has been terminated. Please restart the upload by clicking the button below.', 'storeagent-ai-for-woocommerce' ); ?></p>
            <button type="button" class="button button-primary" id="saai-retry-upload-to-ai-memory"><?php esc_html_e( 'Retry Upload to AI Memory', 'storeagent-ai-for-woocommerce' ); ?></button>
        </div>
    <?php endif; ?>
</div>

<?php if ( ! $has_scheduled ) : ?>
<script>
    jQuery(document).ready(function($) {
        $('#saai-retry-upload-to-ai-memory').on('click', function() {
            $.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) {
                    if (response.success) {
                        window.location.reload();
                    }
                }
            });
        });
    });
</script>
<?php endif; ?>
