<?php
/**
 * 404 log view.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$show_resolved = ! empty( $_GET['resolved'] );
$rows = Seoforge_Not_Found::recent( 100, $show_resolved );
?>

<?php if ( empty( $rows ) && ! $show_resolved ) : ?>
<div class="seoforge-card seoforge-empty-card">
    <span class="seoforge-empty-icon">🎯</span>
    <h2 style="margin-top:0;"><?php esc_html_e( 'No 404s — clean slate.', 'brainwerk-seo-suite' ); ?></h2>
    <p><?php esc_html_e( 'Whenever someone hits a missing page on your site, it shows up here. From here, one click turns it into a redirect — perfect after migrating a site or renaming permalinks.', 'brainwerk-seo-suite' ); ?></p>
</div>
<?php endif; ?>

<div class="seoforge-card">
    <div class="seoforge-row-spread">
        <h2><?php esc_html_e( '404 log', 'brainwerk-seo-suite' ); ?></h2>
        <p>
            <a class="<?php echo ! $show_resolved ? 'is-active' : ''; ?>" href="<?php echo esc_url( add_query_arg( 'resolved', 0 ) ); ?>"><?php esc_html_e( 'Open', 'brainwerk-seo-suite' ); ?></a> ·
            <a class="<?php echo $show_resolved ? 'is-active' : ''; ?>" href="<?php echo esc_url( add_query_arg( 'resolved', 1 ) ); ?>"><?php esc_html_e( 'Resolved', 'brainwerk-seo-suite' ); ?></a>
        </p>
    </div>

    <table class="widefat striped">
        <thead>
            <tr>
                <th><?php esc_html_e( 'URL', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Hits', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Last seen', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Last referrer', 'brainwerk-seo-suite' ); ?></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <?php if ( empty( $rows ) ) : ?>
                <tr><td colspan="5"><?php esc_html_e( 'No 404s logged yet.', 'brainwerk-seo-suite' ); ?></td></tr>
            <?php else : foreach ( $rows as $r ) : ?>
                <tr>
                    <td><a href="<?php echo esc_url( home_url( $r['url_path'] ) ); ?>" target="_blank"><code><?php echo esc_html( $r['url_path'] ); ?></code></a></td>
                    <td><?php echo esc_html( number_format_i18n( $r['hits'] ) ); ?></td>
                    <td><?php echo esc_html( $r['last_seen'] ); ?></td>
                    <td><?php echo esc_html( $r['last_referrer'] ?: '—' ); ?></td>
                    <td>
                        <button type="button" class="button button-small sf-quick-redirect"
                            data-source="<?php echo esc_attr( $r['url_path'] ); ?>"
                            aria-label="<?php echo esc_attr( sprintf( /* translators: %s: 404 URL path */ __( 'Add a redirect for %s', 'brainwerk-seo-suite' ), $r['url_path'] ) ); ?>">
                            <?php esc_html_e( 'Add redirect', 'brainwerk-seo-suite' ); ?>
                        </button>
                        <?php if ( ! $r['is_resolved'] ) : ?>
                            <button type="button" class="button button-small sf-resolve-404" data-id="<?php echo (int) $r['id']; ?>"
                                aria-label="<?php echo esc_attr( sprintf( /* translators: %s: 404 URL path */ __( 'Mark %s as resolved', 'brainwerk-seo-suite' ), $r['url_path'] ) ); ?>">
                                <?php esc_html_e( 'Mark resolved', 'brainwerk-seo-suite' ); ?>
                            </button>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; endif; ?>
        </tbody>
    </table>
</div>
