<?php

defined('ABSPATH') || exit;

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- not relevant

global $wpdb;

// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not relevant
$subpage = sanitize_key($_GET['subpage'] ?? '');

switch ($subpage) {
    case 'logs':
        include __DIR__ . '/logs.php';
        return;
    case 'filters':
        include __DIR__ . '/filters.php';
        return;
    case 'roles':
        include __DIR__ . '/roles.php';
        return;
}

$reg_per_day = $wpdb->get_results("select date(user_registered) as date, count(*) as total from {$wpdb->prefix}users where user_registered > DATE_SUB(NOW(), INTERVAL 365 DAY) group by date(user_registered) order by date(user_registered) asc");
$reg_per_day_x = [];
$reg_per_day_y = [];

foreach ($reg_per_day as $data) {
    $reg_per_day_x[] = $data->date;
    $reg_per_day_y[] = $data->total;
}

?>
<?php include __DIR__ . '/../menu.php'; ?>
<div class="wrap">
    <?php include __DIR__ . '/nav.php'; ?>
    <p>
    </p>

    <div id="graph" style="margin: 2rem 0 0 0"></div>

    <script>
        jQuery(function () {
            var layout = {
                title: {text: 'Users registered per day'}
            };
            var data = [{
                    x: <?php echo json_encode($reg_per_day_x); ?>,
                    y: <?php echo json_encode($reg_per_day_y); ?>,
                    type: 'scatter'
                }];

            Plotly.newPlot('graph', data, layout);
        });
    </script>
</div>
