<?php
/**
 * Audit log view — settings/action history. Free build: last 5 entries as a
 * teaser + upsell. Pro build: full paginated history + CSV export.
 *
 * @package Brainwerk SEO Suite
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$is_pro  = class_exists( 'Seoforge_License' ) && Seoforge_License::is_pro();
$blog_id = Seoforge_Utils::stats_scope(); // int for one site, null for the whole network.

$per_page = 20;
$paged    = isset( $_GET['paged'] ) ? max( 1, (int) $_GET['paged'] ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( $is_pro ) {
    $total = Seoforge_Audit::count( array( 'blog_id' => $blog_id ) );
    $rows  = Seoforge_Audit::query( array( 'blog_id' => $blog_id, 'limit' => $per_page, 'offset' => ( $paged - 1 ) * $per_page ) );
} else {
    $total = min( Seoforge_Audit::count( array( 'blog_id' => $blog_id ) ), Seoforge_Audit::FREE_TEASER_LIMIT );
    $rows  = Seoforge_Audit::query( array( 'blog_id' => $blog_id, 'limit' => Seoforge_Audit::FREE_TEASER_LIMIT, 'offset' => 0 ) );
}
$total_pages = $is_pro ? max( 1, (int) ceil( $total / $per_page ) ) : 1;
?>
<div class="seoforge-card">
    <div class="seoforge-row-spread">
        <h2>📜 <?php esc_html_e( 'Audit log', 'brainwerk-seo-suite' ); ?> <?php echo Seoforge_License::pro_badge(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></h2>
        <?php if ( $is_pro ) : ?>
            <a class="button" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=seoforge_export&type=audit' ), Seoforge_Export::NONCE ) ); ?>">
                <?php esc_html_e( 'Export CSV', 'brainwerk-seo-suite' ); ?>
            </a>
        <?php endif; ?>
    </div>
    <p class="description"><?php esc_html_e( 'A record of settings changes and key actions in this plugin — who did what, and when. Values themselves are not logged, only a short description of what changed.', 'brainwerk-seo-suite' ); ?></p>

    <table class="widefat striped">
        <thead>
            <tr>
                <th><?php esc_html_e( 'When (UTC)', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'User', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Action', 'brainwerk-seo-suite' ); ?></th>
                <th><?php esc_html_e( 'Details', 'brainwerk-seo-suite' ); ?></th>
            </tr>
        </thead>
        <tbody>
            <?php if ( empty( $rows ) ) : ?>
                <tr><td colspan="4"><?php esc_html_e( 'Nothing logged yet.', 'brainwerk-seo-suite' ); ?></td></tr>
            <?php else : foreach ( $rows as $r ) : ?>
                <tr>
                    <td><?php echo esc_html( $r['created_at'] ); ?></td>
                    <td><?php echo esc_html( $r['user_login'] ? $r['user_login'] : '—' ); ?></td>
                    <td><code><?php echo esc_html( $r['action'] ); ?></code></td>
                    <td><?php echo esc_html( $r['summary'] ? $r['summary'] : '—' ); ?></td>
                </tr>
            <?php endforeach; endif; ?>
        </tbody>
    </table>

    <?php if ( $is_pro && $total_pages > 1 ) : ?>
        <p class="seoforge-pagination">
            <?php for ( $p = 1; $p <= $total_pages; $p++ ) : ?>
                <a class="button button-small <?php echo $p === $paged ? 'button-primary' : ''; ?>"
                   href="<?php echo esc_url( add_query_arg( 'paged', $p ) ); ?>"><?php echo (int) $p; ?></a>
            <?php endfor; ?>
        </p>
    <?php endif; ?>

    <?php if ( ! $is_pro ) : ?>
        <div class="seoforge-callout">
            <strong><?php esc_html_e( 'Free plan: showing the last 5 entries.', 'brainwerk-seo-suite' ); ?></strong>
            <p style="margin:4px 0 0;">
                <?php esc_html_e( 'Upgrade to Pro for the full audit history, pagination and CSV export — useful for NIS2/DSGVO documentation of who changed what.', 'brainwerk-seo-suite' ); ?>
                <a href="<?php echo esc_url( Seoforge_License::get()['pricing_url'] ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'See plans', 'brainwerk-seo-suite' ); ?></a>
            </p>
        </div>
    <?php endif; ?>
</div>
