<?php
/**
 * Dashboard overview compiled from each module.
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Modules\Dashboard;

use SuiteMkt3k\Installer;
use SuiteMkt3k\Modules\Statistics\Stats_Aggregator;

defined( 'ABSPATH' ) || exit;

final class Dashboard_Manager {

	public static function register(): void {}

	public static function get_overview(): array {
		global $wpdb;
		$stats = Stats_Aggregator::overview( '30d' );
		$table = Installer::table( 'logs' );
		$logs  = $wpdb->get_results( "SELECT level, context, message, created_at FROM {$table} ORDER BY id DESC LIMIT 5", ARRAY_A ); // phpcs:ignore

		$coupons_total     = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . Installer::table( 'coupons' ) ); // phpcs:ignore
		$newsletters_total = (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . Installer::table( 'newsletters' ) ); // phpcs:ignore
		$carts_pending     = (int) $wpdb->get_var( "SELECT COUNT(*) FROM " . Installer::table( 'abandoned_carts' ) . " WHERE status = 'pending'" ); // phpcs:ignore

		return array(
			'stats'    => $stats,
			'activity' => is_array( $logs ) ? $logs : array(),
			'counters' => array(
				'coupons_total'     => $coupons_total,
				'newsletters_total' => $newsletters_total,
				'carts_pending'     => $carts_pending,
			),
		);
	}
}
