<?php
/**
 * Reconcile View — NEW in v4, updated v4.1
 *
 * Variables in scope:
 *   $misaligned_count - int: number of attachments needing reconciliation
 *   $capped_count     - int: number of attachments that hit retry cap (v4.1)
 *   $is_configured    - bool: whether R2 credentials are present
 *
 * @package Wrenio_Media_Offload
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
?>

<div class="wrenio-media-offload-admin">

    <div class="notice notice-warning" style="border-left-color: #dba617;">
        <p><strong><?php echo esc_html__( 'Destructive operation:', 'wrenio-media-offload' ); ?></strong>
            <?php echo esc_html__( 'Reconciliation moves objects on R2 and deletes the old copies. Take a backup of your database and R2 bucket before running against a production site, and run in dry-run mode first.', 'wrenio-media-offload' ); ?>
        </p>
    </div>

    <div class="card">
        <h2 class="title"><?php echo esc_html__( 'What this does', 'wrenio-media-offload' ); ?></h2>
        <p>
            <?php echo esc_html__( 'Earlier plugin versions generated R2 object keys from the attachment publish date. If a publish date was later changed (manually or by a plugin), the R2 key drifted away from the local file path. This tool detects those mismatches and moves the R2 objects to the correct path via server-side copy, so your R2 bucket mirrors the upload folder structure.', 'wrenio-media-offload' ); ?>
        </p>
        <p><strong><?php echo esc_html__( 'Per attachment, the tool:', 'wrenio-media-offload' ); ?></strong></p>
        <ol style="margin-left: 20px;">
            <li><?php echo esc_html__( 'Copies each R2 object (full + thumbnails) from its current key to the target path.', 'wrenio-media-offload' ); ?></li>
            <li><?php echo esc_html__( 'Verifies the new object exists via HEAD.', 'wrenio-media-offload' ); ?></li>
            <li><?php echo esc_html__( 'Updates WordPress post meta to point at the new keys/URLs.', 'wrenio-media-offload' ); ?></li>
            <li><?php echo esc_html__( 'Deletes the old R2 objects (logged as warnings if delete fails — non-fatal).', 'wrenio-media-offload' ); ?></li>
        </ol>
    </div>

    <div class="card" style="margin-top: 20px;">
        <h2 class="title"><?php echo esc_html__( 'Current Status', 'wrenio-media-offload' ); ?></h2>

        <?php if ( ! $is_configured ) : ?>
            <div class="notice notice-error inline" style="margin: 16px 0; padding: 10px;">
                <p>
                    <?php
                    printf(
                        /* translators: %s: settings page URL */
                        esc_html__( 'R2 is not configured. %s', 'wrenio-media-offload' ),
                        '<a href="' . esc_url( admin_url( 'admin.php?page=wrenio-media-offload' ) ) . '">' . esc_html__( 'Configure R2 credentials', 'wrenio-media-offload' ) . '</a>'
                    );
                    ?>
                </p>
            </div>
        <?php else : ?>
            <table class="widefat striped" style="margin-bottom: 20px;">
                <tbody>
                    <tr>
                        <td><strong><?php echo esc_html__( 'Attachments needing reconciliation', 'wrenio-media-offload' ); ?></strong></td>
                        <td><span id="misaligned-count"><?php echo esc_html( number_format_i18n( $misaligned_count ) ); ?></span></td>
                    </tr>
                    <tr>
                        <td>
                            <strong><?php echo esc_html__( 'Attachments over retry cap', 'wrenio-media-offload' ); ?></strong>
                            <br><span class="description"><?php echo esc_html__( 'Attachments that failed 3+ times are skipped by scans. Use Reset Retries to give them another chance.', 'wrenio-media-offload' ); ?></span>
                        </td>
                        <td>
                            <span id="capped-count"><?php echo esc_html( number_format_i18n( $capped_count ) ); ?></span>
                            <?php if ( wrenio_studio_can_use_pro() && $capped_count > 0 ) : ?>
                                <button type="button" id="reset-retries" class="button button-small" style="margin-left: 10px;">
                                    <?php echo esc_html__( 'Reset Retries', 'wrenio-media-offload' ); ?>
                                </button>
                            <?php endif; ?>
                        </td>
                    </tr>
                </tbody>
            </table>

            <p>
                <button type="button" id="refresh-count" class="button"><?php echo esc_html__( 'Refresh count', 'wrenio-media-offload' ); ?></button>
            </p>

            <h3><?php echo esc_html__( 'Options', 'wrenio-media-offload' ); ?></h3>
            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="reconcile_batch_size"><?php echo esc_html__( 'Batch size', 'wrenio-media-offload' ); ?></label>
                    </th>
                    <td>
                        <input type="number" id="reconcile_batch_size" value="5" min="1" max="20" />
                        <p class="description"><?php echo esc_html__( 'Attachments processed per AJAX call (1–20). Each attachment may involve multiple R2 operations.', 'wrenio-media-offload' ); ?></p>
                    </td>
                </tr>
            </table>

            <p>
                <button type="button" id="start-dry-run" class="button button-secondary" <?php disabled( $misaligned_count === 0 ); ?>>
                    <?php echo esc_html__( 'Start Dry Run', 'wrenio-media-offload' ); ?>
                </button>
                <?php if ( wrenio_studio_can_use_pro() ) : ?>
                    <button type="button" id="start-reconcile" class="button button-primary" <?php disabled( $misaligned_count === 0 ); ?>>
                        <?php echo esc_html__( 'Run Reconciliation', 'wrenio-media-offload' ); ?>
                    </button>
                <?php endif; ?>
                <button type="button" id="stop-reconcile" class="button" style="display: none;">
                    <?php echo esc_html__( 'Stop', 'wrenio-media-offload' ); ?>
                </button>
                <span id="reconcile-status" style="margin-left: 12px;"></span>
            </p>

            <div id="reconcile-progress" style="display: none; margin-top: 16px;">
                <div style="background: #f0f0f1; border-radius: 4px; height: 20px; overflow: hidden;">
                    <div id="reconcile-progress-bar" style="background: #00a32a; height: 100%; width: 0; transition: width 0.3s;"></div>
                </div>
                <p id="reconcile-progress-status" style="margin-top: 6px; font-size: 13px;"></p>
            </div>
        <?php endif; ?>
    </div>

    <?php if ( $is_configured ) : ?>
    <div class="card" style="margin-top: 20px;">
        <h2 class="title"><?php echo esc_html__( 'Activity Log', 'wrenio-media-offload' ); ?></h2>
        <div id="reconcile-log" style="background: #f6f7f7; border: 1px solid #c3c4c7; border-radius: 4px; padding: 10px; max-height: 400px; overflow-y: auto; font-family: monospace; font-size: 12px;"></div>
    </div>
    <?php endif; ?>
</div>
