<?php

if ( $s3_provider !== '' ) {
    ?>
    <div class="upcasted-tools-container <?php 
    echo ( $bucket ? esc_attr( $bucket ) : 'hidden' );
    ?>">
        <h2 class="upcasted-title">Tools</h2>
        <?php 
    ?>
        <div class="upcasted-image-number-container hidden">
            <div class="upcasted-running-tool"><strong>Tool:</strong> <span></span></div>
            <p class="upcasted-total-images-container">
                <span>Total images to move:</span>
                <span class="upcasted-number-of-images"></span>
            </p>
        </div>
        <div class="upcasted-cron-message hidden"></div>
        <div class="upcasted-tools-error hidden"></div>
        <div class="upcasted-tools-option local-to-s3-cron">
            <div class="upcasted-tools-option-header">
                <div class="upcasted-tools-tool-name">
                    <span><strong>
                        Migrate files to S3 bucket<?php 
    echo ( uso_fs()->is_not_paying() ? '[Premium feature]' : '' );
    ?>
                    </strong> <br/>
                    <strong>[</strong> From current server <strong>to</strong> S3 bucket <strong>]</strong></span>
                </div>
                <div class="upcasted-tools-tool-actions">
                    <?php 
    if ( uso_fs()->is_not_paying() ) {
        ?>
                        <a href="<?php 
        echo uso_fs()->get_upgrade_url();
        ?>"
                            class="upcasted-button upcasted-upgrade-plan">Upgrade now</a>
                    <?php 
    } else {
        ?>
                        <button id="cron-local-to-s3-button"
                                class="upcasted-button upcasted-tool-button  <?php 
        echo ( $run_local_to_s3_cron !== '' ? 'upcasted-stop-action-event upcasted-stop-action' : '' );
        ?>">
                            <?php 
        echo ( $run_local_to_s3_cron !== '' ? 'Stop' : 'Start' );
        ?>
                        </button>
                    <?php 
    }
    ?>
                </div>
            </div>
            <div class="upcasted-tools-option-footer">
                <button class="upcasted-find-out-more"><span class="dashicons dashicons-warning"></span>
                    More info
                </button>
                <div class="upcasted-hidden-help-description">
                    <p>This tool runs in the background using default WordPress CRON. You can exit the
                        browser window and the file migration will still run.</p>
                </div>
            </div>
        </div>
        <div class="upcasted-tools-option s3-to-local-cron">
            <div class="upcasted-tools-option-header">
                <div class="upcasted-tools-tool-name">
                    <span><strong>Migrate files back to current server <?php 
    echo ( uso_fs()->is_not_paying() ? '[Premium feature]' : '' );
    ?></strong> <br/>
                    <strong>[</strong> From S3 bucket <strong>to</strong> current server <strong>]</strong></span>
                </div>
                <div class="upcasted-tools-tool-actions">
                    <?php 
    if ( uso_fs()->is_not_paying() ) {
        ?>
                        <a href="<?php 
        echo uso_fs()->get_upgrade_url();
        ?>"
                            class="upcasted-button upcasted-upgrade-plan">Upgrade now</a>
                    <?php 
    } else {
        ?>
                        <button id="cron-s3-to-local-button"
                                class="upcasted-button upcasted-tool-button <?php 
        echo ( $run_s3_to_local_cron !== '' ? 'upcasted-stop-action-event upcasted-stop-action' : '' );
        ?>">
                            <?php 
        echo ( $run_s3_to_local_cron !== '' ? 'Stop' : 'Start' );
        ?>
                        </button>
                    <?php 
    }
    ?>
                </div>
            </div>
            <div class="upcasted-tools-option-footer">
                <button class="upcasted-find-out-more"><span class="dashicons dashicons-warning"></span>
                    More info
                </button>
                <div class="upcasted-hidden-help-description">
                    <p>This tool runs in the background using default WordPress CRON. You can exit the
                        browser window and the file migration will still run.</p>
                </div>
            </div>
        </div>
        <div class="upcasted-tools-option upcasted-define-batch">
            <div class="upcasted-tools-option-header">
                <div class="upcasted-tools-tool-name">
                    <span><strong>Define batch size <?php 
    echo ( uso_fs()->is_not_paying() ? '[Premium feature]' : '' );
    ?></strong></span>
                </div>
            </div>
            <div class="upcasted-tools-option-body">
                <input type="number" name="upcasted-custom-batch-size"
                        id="custom-batch-size"
                        placeholder="Default is 5"
                        value="<?php 
    echo esc_attr( $custom_batch_size );
    ?>">
                <?php 
    if ( uso_fs()->is_not_paying() ) {
        ?>
                    <a href="<?php 
        echo uso_fs()->get_upgrade_url();
        ?>"
                        class="upcasted-button upcasted-upgrade-plan">Upgrade now</a>
                <?php 
    } else {
        ?>
                    <button id="custom-batch-size-button" class="upcasted-button <?php 
        echo ( $run_s3_to_local_cron !== '' || $run_local_to_s3_cron !== '' ? 'upcasted-stop-action-event' : '' );
        ?>">Save</button>
                    <div class="batch-size-message"></div>
                <?php 
    }
    ?>
            </div>
            <div class="upcasted-tools-option-footer">
                <button class="upcasted-find-out-more"><span class="dashicons dashicons-editor-help"></span>
                    Need help?
                </button>
                <div class="upcasted-hidden-help-description">
                    <p>This setting controls how many files are transferred during each operation, helping you manage the efficiency of your file transfers.</p>
                    <p>For example, when transferring a single image with multiple resized versions:</p>
                    <ul>
                        <li><strong>Batch size set to 1:</strong> Each transfer moves five files (the original image plus its four resized versions).</li>
                        <li><strong>Batch size set to 5:</strong> Each transfer moves 25 files (five original images along with their resized versions).</li>
                    </ul>
                    <p>Larger batch sizes can significantly improve transfer speeds, especially on high-performance servers, by allowing multiple files to be processed simultaneously.</p>
                    <p><strong>Important Note:</strong> This feature is intended for advanced users. Adjusting batch sizes improperly could overwhelm your server or impact performance. Use it thoughtfully!</p>
                </div>
            </div>
        </div>
    <?php 
    if ( !uso_fs()->is_not_paying() ) {
        ?>
        <!-- Migration Status Checker Tool -->
        <div class="upcasted-tools-option upcasted-migration-status-checker">
            <div class="upcasted-tools-option-header">
                <div class="upcasted-tools-tool-name">
                    <span><strong>Migration Status Checker</strong></span>
                </div>
            </div>
            <div class="upcasted-tools-option-body">
                <p>Check the current migration status of all images and identify which files are not properly synchronized with S3.</p>
                
                <div class="upcasted-migration-checker-options">
                    <label style="display: block; margin-bottom: 10px;">
                        <strong>Check Direction:</strong>
                    </label>
                    <label style="display: block; margin-bottom: 8px;">
                        <input type="radio" name="upcasted-check-direction" value="local_to_cloud" checked>
                        Find files missing on S3 (should be uploaded)
                    </label>
                    <label style="display: block; margin-bottom: 12px;">
                        <input type="radio" name="upcasted-check-direction" value="cloud_to_local">
                        Find files missing locally (should be downloaded)
                    </label>
                    <label>
                        <input type="checkbox" id="upcasted-check-webp" checked>
                        Also check WebP versions of images
                    </label>
                </div>

                <div id="upcasted-migration-status-message" class="notice" style="display:none; margin: 15px 0; padding: 10px; position: relative;">
                    <button type="button" class="notice-dismiss" id="upcasted-dismiss-status-message" style="position: absolute; right: 1px; top: 0; border: none; background: none; color: #787c82; cursor: pointer;">
                        <span class="screen-reader-text">Dismiss this notice.</span>
                        <span class="dashicons dashicons-dismiss"></span>
                    </button>
                    <div id="upcasted-migration-status-content"></div>
                </div>

                <div id="upcasted-migration-checker-progress" style="display:none; margin: 15px 0;">
                    <div class="upcasted-progress-wrapper">
                        <div class="upcasted-progress-bar" style="position:relative;width:100%;border:1px solid #ccd0d4;height:24px;border-radius:6px;background:#f8f9f9;overflow:hidden;">
                            <div id="upcasted-checker-progress-fill" style="background:#0073aa;height:100%;width:0%;transition:width .5s ease;display:flex;align-items:center;justify-content:center;color:#fff;font-weight:600;font-size:12px;">
                                <span id="upcasted-checker-progress-text">Checking...</span>
                            </div>
                        </div>
                    </div>
                    <p id="upcasted-checker-status">Initializing migration status check...</p>
                </div>
            </div>
            <div class="upcasted-tools-option-footer">
                <button class="upcasted-button upcasted-tool-button" id="upcasted-run-migration-check">
                    Start Migration Status Check
                </button>
                <button class="upcasted-button upcasted-tool-button upcasted-stop-action" id="upcasted-stop-migration-check" style="display:none;">
                    Stop Check
                </button>
                <button class="upcasted-find-out-more"><span class="dashicons dashicons-info"></span> More info</button>
                <div class="upcasted-hidden-help-description">
                    <p>This tool runs a comprehensive background analysis of your media library to identify synchronization issues:</p>
                    <ul>
                        <li><strong>Background processing:</strong> Runs efficiently in the background without blocking your admin interface</li>
                        <li><strong>Bidirectional checking:</strong> Check for files missing from S3 (need upload) or missing from server (need download)</li>
                        <li><strong>Missing files:</strong> Identifies main images, thumbnails, and WebP variants that are out of sync</li>
                        <li><strong>Partial migrations:</strong> Detects incomplete migrations where only some files were transferred</li>
                        <li><strong>WebP analysis:</strong> Optional check for WebP versions of your images</li>
                        <li><strong>Persistent results:</strong> Results stay visible even after page refresh until next check</li>
                    </ul>
                    <p><strong>Perfect for large sites:</strong> Efficiently processes hundreds of thousands of images without timeouts or memory issues.</p>
                </div>
            </div>
        </div>
        <div class="upcasted-tools-option upcasted-migration-locks">
            <div class="upcasted-tools-option-header">
                <div class="upcasted-tools-tool-name">
                    <span><strong>Migration Locks (Diagnostics)</strong></span>
                </div>
                <div class="upcasted-tools-tool-actions">
                    <button id="upcasted-refresh-lock-status" class="upcasted-button upcasted-tool-button upcasted-exempt-disable">Refresh status</button>
                </div>
            </div>
            <div class="upcasted-tools-option-body">
                <div class="upcasted-lock-status-table">
                    <table class="widefat striped">
                        <thead>
                            <tr>
                                <th>Direction</th>
                                <th>Active</th>
                                <th>Started</th>
                                <th>Last Touch (Age)</th>
                                <th>PID</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody id="upcasted-lock-status-body">
                            <tr><td colspan="6">No data yet. Click "Refresh status".</td></tr>
                        </tbody>
                    </table>
                </div>
                <div class="upcasted-lock-status-message"></div>
            </div>
            <div class="upcasted-tools-option-footer">
                <button class="upcasted-find-out-more"><span class="dashicons dashicons-editor-help"></span> Need help?</button>
                <div class="upcasted-hidden-help-description">
                    <p>This diagnostic shows the internal migration mutex locks. If a lock remains active for too long (e.g., due to a fatal error), you can force release it.</p>
                    <p><strong>Warning:</strong> Forcing unlock while a legitimate migration is running can cause overlapping processes. Use only if you are certain the process is stuck.</p>
                </div>
            </div>
        </div>
        <?php 
        // Progress bar moved earlier; removed duplicate here.
        ?>
    <?php 
    }
    ?>
    </div>
<?php 
}