<?php
/**
 * The widget on the WordPress dashboard.
 *
 * @package TextTango
 */

declare( strict_types=1 );

namespace TextTango\Admin;

use TextTango\Analytics\Stats;
use TextTango\Install\Activator;

defined( 'ABSPATH' ) || exit;

/**
 * A summary of what the site has been sending, on the screen an admin already
 * opens each morning (spec §11).
 *
 * ### Why it is gated, and why the figures are cached
 *
 * The WordPress dashboard is the one admin screen everybody lands on, including
 * authors, editors and shop managers who have nothing to do with SMS. So the
 * widget is registered only for users holding `manage_texttango`, rather than
 * registered and then hidden — a widget somebody cannot use is still a widget
 * they have to dismiss, and one whose figures they should not see at all.
 *
 * And because this runs for every one of those users on every dashboard load,
 * the aggregates behind it are cached rather than recomputed. They are counts
 * over a table with no retention ceiling by default.
 */
final class DashboardWidget {

	/**
	 * The widget's id, as WordPress knows it.
	 */
	public const ID = 'texttango_overview';

	/**
	 * Register hooks.
	 */
	public function register(): void {
		add_action( 'wp_dashboard_setup', array( $this, 'add' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'styles' ) );
	}

	/**
	 * The stylesheet, on the dashboard only.
	 *
	 * `Menu::enqueue_assets()` loads assets on the plugin's own screens, and the
	 * dashboard is not one of them — so without this the widget renders as
	 * unstyled markup on the first screen a merchant ever sees.
	 *
	 * The stylesheet alone. The widget has no behaviour, so the 23 KB of
	 * JavaScript that drives the settings screens would be loaded on every
	 * dashboard in the site to do nothing at all.
	 *
	 * @param string $hook_suffix Screen the request is for.
	 */
	public function styles( string $hook_suffix ): void {
		if ( 'index.php' !== $hook_suffix || ! current_user_can( Activator::CAPABILITY ) ) {
			return;
		}

		wp_enqueue_style( 'texttango-admin', TEXTTANGO_URL . 'assets/admin/admin.css', array(), TEXTTANGO_VERSION );
	}

	/**
	 * Add the widget, for those who should see it.
	 */
	public function add(): void {
		if ( ! current_user_can( Activator::CAPABILITY ) ) {
			return;
		}

		wp_add_dashboard_widget( self::ID, __( 'TextTango', 'texttango' ), array( $this, 'render' ) );
	}

	/**
	 * Draw it.
	 */
	public function render(): void {
		// Checked again here rather than trusted from registration: a widget can
		// be rendered by anything that knows its callback.
		if ( ! current_user_can( Activator::CAPABILITY ) ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only view state; Stats validates it against a fixed list of windows.
		$days = Stats::window_from_request( wp_unslash( $_GET ) );

		$windows = array();

		foreach ( Stats::WINDOWS as $window ) {
			// Back to the dashboard itself, since that is where this renders.
			$windows[ (string) $window ] = add_query_arg( Stats::ARG, $window, admin_url( 'index.php' ) );
		}

		load_template(
			TEXTTANGO_PATH . 'templates/admin/partials/analytics.php',
			false,
			array(
				'stats'   => Stats::summary( $days ),
				'compact' => true,
				'tones'   => Stats::tones(),
				'windows' => $windows,
				'links'   => array(
					__( 'Analytics', 'texttango' )   => AnalyticsPage::url(),
					__( 'Message log', 'texttango' ) => Tabs::url( Tabs::LOGS ),
					__( 'Campaigns', 'texttango' )   => CampaignsPage::url(),
				),
			)
		);
	}
}
