<?php

/**
 * Public-safe activity log summary counts for themes and the stats API.
 */

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

class Sentinel_Public_Stats
{
    /**
     * Summary counts safe to show on public pages (no user names, IPs, or log lines).
     *
     * @return array{total_logs:int,today_logs:int,logins_today:int}
     */
    public static function get_summary()
    {
        global $wpdb;

        $table = $wpdb->prefix . 'sentinel_logs';
        $today_start = current_time('Y-m-d') . ' 00:00:00';
        $today_end = current_time('Y-m-d') . ' 23:59:59';

        $total_logs = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}");
        $today_logs = (int) $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(*) FROM {$table} WHERE created_at BETWEEN %s AND %s",
                $today_start,
                $today_end
            )
        );
        $logins_today = (int) $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(*) FROM {$table} WHERE event_key = %s AND created_at BETWEEN %s AND %s",
                'user_login',
                $today_start,
                $today_end
            )
        );

        return array(
            'total_logs'    => $total_logs,
            'today_logs'    => $today_logs,
            'logins_today'  => $logins_today,
        );
    }

    /**
     * @return int
     */
    public static function get_total_logs()
    {
        return self::get_summary()['total_logs'];
    }

    /**
     * @return int
     */
    public static function get_today_logs()
    {
        return self::get_summary()['today_logs'];
    }

    /**
     * @return int
     */
    public static function get_logins_today()
    {
        return self::get_summary()['logins_today'];
    }
}
