<?php
/**
 * Multisite aggregation: per-site stats and network totals.
 *
 * @package Brainwerk Security Suite
 */

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

class Shieldforge_Multisite {

    public static function register( Shieldforge_Loader $loader ) {
        // Read-only module; no hooks required for now.
        unset( $loader );
    }

    public static function is_available() {
        return is_multisite();
    }

    public static function list_sites_with_stats( $hours = 24 ) {
        if ( ! self::is_available() ) {
            return array();
        }
        $sites  = get_sites( array( 'number' => 200 ) );
        $since  = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS * (int) $hours );
        $result = array();

        foreach ( $sites as $site ) {
            $blog_id = (int) $site->blog_id;
            $result[] = array(
                'blog_id'    => $blog_id,
                'domain'     => $site->domain,
                'path'       => $site->path,
                'name'       => get_blog_option( $blog_id, 'blogname', $site->domain ),
                'fails'      => Shieldforge_Logger::count_since( $since, array(
                    'event_type' => Shieldforge_Logger::EVT_LOGIN_FAIL,
                    'blog_id'    => $blog_id,
                ) ),
                'lockouts'   => Shieldforge_Logger::count_since( $since, array(
                    'event_type' => Shieldforge_Logger::EVT_BF_LOCKOUT,
                    'blog_id'    => $blog_id,
                ) ),
                'probes'     => Shieldforge_Logger::count_since( $since, array(
                    'event_type' => Shieldforge_Logger::EVT_PROBING_404,
                    'blog_id'    => $blog_id,
                ) ),
                'has_override' => self::site_has_override( $blog_id ),
            );
        }
        return $result;
    }

    public static function network_totals( $hours = 24 ) {
        $since = gmdate( 'Y-m-d H:i:s', time() - HOUR_IN_SECONDS * (int) $hours );
        return array(
            'fails'    => Shieldforge_Logger::count_since( $since, array( 'event_type' => Shieldforge_Logger::EVT_LOGIN_FAIL ) ),
            'lockouts' => Shieldforge_Logger::count_since( $since, array( 'event_type' => Shieldforge_Logger::EVT_BF_LOCKOUT ) ),
            'probes'   => Shieldforge_Logger::count_since( $since, array( 'event_type' => Shieldforge_Logger::EVT_PROBING_404 ) ),
            'sites'    => self::is_available() ? (int) get_blog_count() : 1,
        );
    }

    public static function site_has_override( $blog_id ) {
        if ( ! self::is_available() ) {
            return false;
        }
        $opt = get_blog_option( (int) $blog_id, SHIELDFORGE_OPTIONS_KEY, null );
        return is_array( $opt ) && ! empty( $opt );
    }

    public static function clear_site_override( $blog_id ) {
        if ( ! self::is_available() ) {
            return false;
        }
        return delete_blog_option( (int) $blog_id, SHIELDFORGE_OPTIONS_KEY );
    }
}
