<?php
/**
 * Site Health tests.
 *
 * @package Form_Flight_Recorder
 */

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

/**
 * Site Health integration.
 */
class Ffrd_Site_Health {

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

	/**
	 * Constructor.
	 *
	 * @param Ffrd_Plugin $plugin Container.
	 */
	public function __construct( $plugin ) {
		$this->plugin = $plugin;
		add_filter( 'site_status_tests', array( $this, 'register_tests' ) );
	}

	/**
	 * Register direct tests.
	 *
	 * @param array $tests Tests.
	 * @return array
	 */
	public function register_tests( $tests ) {
		$tests['direct']['ffrd_missing'] = array(
			'label' => __( 'Every form submission produced its notification', 'form-flight-recorder' ),
			'test'  => array( $this, 'test_missing' ),
		);
		$tests['direct']['ffrd_failed'] = array(
			'label' => __( 'Form notification emails are not failing', 'form-flight-recorder' ),
			'test'  => array( $this, 'test_failed' ),
		);
		$tests['direct']['ffrd_silent'] = array(
			'label' => __( 'No form has gone silent', 'form-flight-recorder' ),
			'test'  => array( $this, 'test_silent' ),
		);
		return $tests;
	}

	/**
	 * Shared result scaffold.
	 *
	 * @param string $label       Label.
	 * @param string $status      good|recommended|critical.
	 * @param string $description Description.
	 * @return array
	 */
	private function result( $label, $status, $description ) {
		return array(
			'label'       => $label,
			'status'      => $status,
			'badge'       => array(
				'label' => __( 'Forms', 'form-flight-recorder' ),
				'color' => 'critical' === $status ? 'red' : 'blue',
			),
			'description' => '<p>' . esc_html( $description ) . '</p>',
			'actions'     => '<a href="' . esc_url( admin_url( 'admin.php?page=ffrd' ) ) . '">' . esc_html__( 'Open Form Flight Recorder', 'form-flight-recorder' ) . '</a>',
			'test'        => 'ffrd',
		);
	}

	/**
	 * Missing notifications.
	 *
	 * @return array
	 */
	public function test_missing() {
		$missing = $this->plugin->get( 'events' )->count( 'notification_missing', time() - 7 * DAY_IN_SECONDS );
		if ( $missing > 0 ) {
			return $this->result(
				__( 'Form submissions produced NO notification', 'form-flight-recorder' ),
				'critical',
				sprintf(
					/* translators: %d: number of submissions. */
					__( 'In the last 7 days, %d completed submission(s) that expected a notification generated no email at all. Those leads reached nobody.', 'form-flight-recorder' ),
					$missing
				)
			);
		}
		return $this->result(
			__( 'Every form submission produced its notification', 'form-flight-recorder' ),
			'good',
			__( 'Every completed submission that expected a notification produced one in the last 7 days.', 'form-flight-recorder' )
		);
	}

	/**
	 * Failed notifications.
	 *
	 * @return array
	 */
	public function test_failed() {
		$failed = $this->plugin->get( 'events' )->count( 'notification_failed', time() - 7 * DAY_IN_SECONDS );
		if ( $failed > 0 ) {
			return $this->result(
				__( 'Form notification emails are failing', 'form-flight-recorder' ),
				'critical',
				sprintf(
					/* translators: %d: number of failures. */
					__( '%d form notification(s) failed at the mail transport in the last 7 days. Check your SMTP/mail plugin configuration.', 'form-flight-recorder' ),
					$failed
				)
			);
		}
		return $this->result(
			__( 'Form notification emails are not failing', 'form-flight-recorder' ),
			'good',
			__( 'No form notification failed at the transport in the last 7 days.', 'form-flight-recorder' )
		);
	}

	/**
	 * Silent forms.
	 *
	 * @return array
	 */
	public function test_silent() {
		$counts = $this->plugin->get( 'reconciler' )->counts();
		if ( ! empty( $counts['silent'] ) ) {
			return $this->result(
				__( 'A form has gone silent', 'form-flight-recorder' ),
				'recommended',
				sprintf(
					/* translators: %d: number of forms. */
					__( '%d form(s) that normally receive submissions have gone quiet beyond their threshold. A broken form looks exactly like a quiet day — check the page.', 'form-flight-recorder' ),
					count( $counts['silent'] )
				)
			);
		}
		return $this->result(
			__( 'No form has gone silent', 'form-flight-recorder' ),
			'good',
			__( 'Every form with a meaningful baseline is still receiving submissions.', 'form-flight-recorder' )
		);
	}
}
