<?php
/**
 * WP-CLI command (free — JSON included).
 *
 * @package Form_Flight_Recorder
 */

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

/**
 * `wp ffrd status` command.
 */
class Ffrd_Cli {

	/**
	 * Container.
	 *
	 * @var Ffrd_Plugin
	 */
	private static $plugin;

	/**
	 * Register with WP-CLI.
	 *
	 * @param Ffrd_Plugin $plugin Container.
	 */
	public static function register( $plugin ) {
		self::$plugin = $plugin;
		WP_CLI::add_command( 'ffrd status', array( __CLASS__, 'status' ) );
	}

	/**
	 * Print the reconciliation status.
	 *
	 * ## OPTIONS
	 *
	 * [--format=<format>]
	 * : table (default) or json.
	 *
	 * @param array $args       Positional args.
	 * @param array $assoc_args Assoc args.
	 */
	public static function status( $args, $assoc_args ) {
		$counts = self::$plugin->get( 'reconciler' )->counts();

		$format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table';

		if ( 'json' === $format ) {
			WP_CLI::line( (string) wp_json_encode( $counts ) );
			return;
		}

		WP_CLI::line( sprintf( 'Forms known: %d (adapters: %s)', $counts['forms'], implode( ',', $counts['detected'] ) ) );
		WP_CLI::line( sprintf( 'Submissions: %d in 24h, %d in 7d', $counts['submissions_24h'], $counts['submissions_7d'] ) );
		WP_CLI::line( sprintf( 'Problems 7d: %d missing notifications, %d failed sends, %d silent forms', $counts['missing_7d'], $counts['failed_7d'], count( $counts['silent'] ) ) );
	}
}
