<?php
/**
 * Filename Cleanup View — NEW in v4.1
 *
 * Variables in scope:
 *   $mismatch_count - int: number of attachments with filename mismatches
 *   $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__( 'Modifies post meta:', 'wrenio-media-offload' ); ?></strong>
            <?php echo esc_html__( 'This tool updates the `_wp_attached_file` post meta to match the filename on R2. It does not modify R2 objects or physical files on disk. Take a database backup before running against a production site.', '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__( 'Detects attachments where the filename portion of `_wp_attached_file` (what WordPress knows as the local path) disagrees with the filename portion of `_r2_key` (the R2 object key). This typically happens when a filename was normalized differently by WordPress vs. R2 at upload time — e.g. a filename like `image..jpg` being stored on R2 but WordPress later normalizing it to `image.jpg`.', '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__( 'Collapses runs of consecutive dots in the local filename to a single dot.', 'wrenio-media-offload' ); ?></li>
            <li><?php echo esc_html__( 'If that normalization still does not match the R2 key filename, falls back to using the R2 filename directly.', 'wrenio-media-offload' ); ?></li>
            <li><?php echo esc_html__( 'Updates `_wp_attached_file` post meta. The directory portion is preserved.', 'wrenio-media-offload' ); ?></li>
        </ol>
        <p>
            <strong><?php echo esc_html__( 'Run R2 Reconcile FIRST.', 'wrenio-media-offload' ); ?></strong>
            <?php echo esc_html__( 'Reconcile handles path-level mismatches (directory portion). Filename Cleanup handles the remaining filename-portion mismatches that Reconcile does not address.', 'wrenio-media-offload' ); ?>
        </p>
    </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 with filename mismatches', 'wrenio-media-offload' ); ?></strong></td>
                        <td><span id="filename-mismatch-count"><?php echo esc_html( number_format_i18n( $mismatch_count ) ); ?></span></td>
                    </tr>
                </tbody>
            </table>

            <p>
                <button type="button" id="filename-refresh-count" class="button"><?php echo esc_html__( 'Refresh count', 'wrenio-media-offload' ); ?></button>
                <button type="button" id="filename-preview" class="button" <?php disabled( $mismatch_count === 0 ); ?>><?php echo esc_html__( 'Preview (first 20)', 'wrenio-media-offload' ); ?></button>
            </p>

            <div id="filename-preview-container" style="display: none; margin-top: 16px;">
                <h3><?php echo esc_html__( 'Preview', 'wrenio-media-offload' ); ?></h3>
                <table class="widefat striped">
                    <thead>
                        <tr>
                            <th><?php echo esc_html__( 'ID', 'wrenio-media-offload' ); ?></th>
                            <th><?php echo esc_html__( 'Title', 'wrenio-media-offload' ); ?></th>
                            <th><?php echo esc_html__( 'Current file', 'wrenio-media-offload' ); ?></th>
                            <th><?php echo esc_html__( 'R2 key', 'wrenio-media-offload' ); ?></th>
                            <th><?php echo esc_html__( 'Proposed', 'wrenio-media-offload' ); ?></th>
                        </tr>
                    </thead>
                    <tbody id="filename-preview-rows"></tbody>
                </table>
            </div>

            <h3 style="margin-top: 20px;"><?php echo esc_html__( 'Options', 'wrenio-media-offload' ); ?></h3>
            <table class="form-table">
                <tr>
                    <th scope="row">
                        <label for="filename_batch_size"><?php echo esc_html__( 'Batch size', 'wrenio-media-offload' ); ?></label>
                    </th>
                    <td>
                        <input type="number" id="filename_batch_size" value="25" min="1" max="100" />
                        <p class="description"><?php echo esc_html__( 'Attachments processed per AJAX call (1–100).', 'wrenio-media-offload' ); ?></p>
                    </td>
                </tr>
            </table>

            <p>
                <button type="button" id="filename-start-repair" class="button button-primary" <?php disabled( $mismatch_count === 0 ); ?>>
                    <?php echo esc_html__( 'Repair All', 'wrenio-media-offload' ); ?>
                </button>
                <button type="button" id="filename-stop-repair" class="button" style="display: none;">
                    <?php echo esc_html__( 'Stop', 'wrenio-media-offload' ); ?>
                </button>
                <span id="filename-status" style="margin-left: 12px;"></span>
            </p>

            <div id="filename-progress" style="display: none; margin-top: 16px;">
                <div style="background: #f0f0f1; border-radius: 4px; height: 20px; overflow: hidden;">
                    <div id="filename-progress-bar" style="background: #00a32a; height: 100%; width: 0; transition: width 0.3s;"></div>
                </div>
                <p id="filename-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="filename-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>
