<?php
/**
 * Multisite tab: per-site stats and override management.
 *
 * @package Brainwerk Security Suite
 */

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

if ( ! Shieldforge_Multisite::is_available() ) :
    ?>
    <div class="shieldforge-card">
        <h2><?php esc_html_e( 'Multisite', 'brainwerk-security-suite' ); ?></h2>
        <p class="muted">
            <?php esc_html_e( 'This installation is not a Multisite network. The Multisite features (per-site stats, network-wide blocks, per-site overrides) are unavailable.', 'brainwerk-security-suite' ); ?>
        </p>
    </div>
    <?php
    return;
endif;

if ( ! empty( $_POST['shieldforge_ms_action'] ) && check_admin_referer( 'shieldforge_ms' ) && current_user_can( 'manage_network_options' ) ) {
    if ( 'clear_override' === sanitize_key( wp_unslash( $_POST['shieldforge_ms_action'] ) ) ) {
        $blog = absint( wp_unslash( $_POST['blog_id'] ?? 0 ) );
        if ( $blog > 0 ) {
            Shieldforge_Multisite::clear_site_override( $blog );
            add_settings_error( 'brainwerk-security-suite', 'sf_override_cleared', sprintf( __( 'Override cleared for site #%d.', 'brainwerk-security-suite' ), $blog ), 'updated' );
        }
    }
}

$totals = Shieldforge_Multisite::network_totals( 24 );
$sites  = Shieldforge_Multisite::list_sites_with_stats( 24 );
?>
<div class="shieldforge-card">
    <h2><?php esc_html_e( 'Network totals (last 24 h)', 'brainwerk-security-suite' ); ?></h2>
    <div class="sf-stats-grid">
        <div class="sf-stat"><span class="sf-stat-num"><?php echo esc_html( number_format_i18n( $totals['sites'] ) ); ?></span><span class="sf-stat-label"><?php esc_html_e( 'Sites', 'brainwerk-security-suite' ); ?></span></div>
        <div class="sf-stat"><span class="sf-stat-num"><?php echo esc_html( number_format_i18n( $totals['fails'] ) ); ?></span><span class="sf-stat-label"><?php esc_html_e( 'Failed logins', 'brainwerk-security-suite' ); ?></span></div>
        <div class="sf-stat"><span class="sf-stat-num"><?php echo esc_html( number_format_i18n( $totals['lockouts'] ) ); ?></span><span class="sf-stat-label"><?php esc_html_e( 'Lockouts', 'brainwerk-security-suite' ); ?></span></div>
        <div class="sf-stat"><span class="sf-stat-num"><?php echo esc_html( number_format_i18n( $totals['probes'] ) ); ?></span><span class="sf-stat-label"><?php esc_html_e( '404 probes', 'brainwerk-security-suite' ); ?></span></div>
    </div>
</div>

<div class="shieldforge-card">
    <h2><?php esc_html_e( 'Per-site activity', 'brainwerk-security-suite' ); ?></h2>
    <?php if ( empty( $sites ) ) : ?>
        <p class="muted"><?php esc_html_e( 'No sites found.', 'brainwerk-security-suite' ); ?></p>
    <?php else : ?>
        <table class="widefat striped">
            <thead>
                <tr>
                    <th>#</th>
                    <th><?php esc_html_e( 'Site', 'brainwerk-security-suite' ); ?></th>
                    <th><?php esc_html_e( 'Failed logins', 'brainwerk-security-suite' ); ?></th>
                    <th><?php esc_html_e( 'Lockouts', 'brainwerk-security-suite' ); ?></th>
                    <th><?php esc_html_e( '404 probes', 'brainwerk-security-suite' ); ?></th>
                    <th><?php esc_html_e( 'Override', 'brainwerk-security-suite' ); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ( $sites as $s ) : ?>
                    <tr>
                        <td><?php echo (int) $s['blog_id']; ?></td>
                        <td>
                            <strong><?php echo esc_html( $s['name'] ); ?></strong><br>
                            <span class="muted"><?php echo esc_html( $s['domain'] . $s['path'] ); ?></span>
                        </td>
                        <td><?php echo esc_html( number_format_i18n( $s['fails'] ) ); ?></td>
                        <td><?php echo esc_html( number_format_i18n( $s['lockouts'] ) ); ?></td>
                        <td><?php echo esc_html( number_format_i18n( $s['probes'] ) ); ?></td>
                        <td>
                            <?php if ( $s['has_override'] ) : ?>
                                <form method="post" style="display:inline" onsubmit="return confirm('<?php echo esc_js( __( 'Clear override and revert to network defaults?', 'brainwerk-security-suite' ) ); ?>');">
                                    <?php wp_nonce_field( 'shieldforge_ms' ); ?>
                                    <input type="hidden" name="shieldforge_ms_action" value="clear_override">
                                    <input type="hidden" name="blog_id" value="<?php echo (int) $s['blog_id']; ?>">
                                    <button type="submit" class="button button-small"><?php esc_html_e( 'Clear', 'brainwerk-security-suite' ); ?></button>
                                </form>
                            <?php else : ?>
                                <span class="muted">—</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>

<?php
/**
 * Extension point for Pro: cross-site attacker correlation, per-site threat
 * scores, network-wide 2FA coverage, sparklines etc. Free renders nothing
 * here; Pro hooks in.
 */
do_action( 'shieldforge_multisite_after_cards' );
?>
