<?php
/**
 * Site Health integration: two recommended-tier tests.
 *
 * @package SEO_Flight_Recorder
 */

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

/**
 * Class SFRec_Site_Health
 */
class SFRec_Site_Health {

	/**
	 * Wire the tests.
	 */
	public static function init() {
		add_filter( 'site_status_tests', array( __CLASS__, 'register_tests' ) );
	}

	/**
	 * Register.
	 *
	 * @param array $tests Site Health tests.
	 * @return array
	 */
	public static function register_tests( $tests ) {
		$tests['direct']['sfrec_monitoring'] = array(
			'label' => __( 'SEO output monitoring', 'buildsbyluke-seo-flight-recorder' ),
			'test'  => array( __CLASS__, 'test_monitoring' ),
		);
		$tests['direct']['sfrec_findings'] = array(
			'label' => __( 'Open critical SEO findings', 'buildsbyluke-seo-flight-recorder' ),
			'test'  => array( __CLASS__, 'test_findings' ),
		);
		return $tests;
	}

	/**
	 * Test 1: is monitoring able to run at all?
	 *
	 * @return array
	 */
	public static function test_monitoring() {
		$state  = get_option( 'sfrec_state', array() );
		$result = array(
			'label'       => __( 'SEO Flight Recorder can monitor this site', 'buildsbyluke-seo-flight-recorder' ),
			'status'      => 'good',
			'badge'       => array(
				'label' => __( 'SEO', 'buildsbyluke-seo-flight-recorder' ),
				'color' => 'blue',
			),
			'description' => '<p>' . esc_html__( 'Loopback requests work, so scheduled checks can fetch and verify this site\'s rendered SEO output.', 'buildsbyluke-seo-flight-recorder' ) . '</p>',
			'actions'     => '',
			'test'        => 'sfrec_monitoring',
		);

		if ( isset( $state['loopback_ok'] ) && false === $state['loopback_ok'] ) {
			$result['status']      = 'recommended';
			$result['label']       = __( 'SEO Flight Recorder cannot fetch this site\'s own pages', 'buildsbyluke-seo-flight-recorder' );
			$result['description'] = '<p>' . esc_html__( 'The last loopback self-check failed, so SEO output checks cannot run. This is usually caused by password protection (Basic Auth), a firewall rule blocking self-requests, or an SSL misconfiguration.', 'buildsbyluke-seo-flight-recorder' ) . '</p>' . ( ! empty( $state['loopback_error'] ) ? '<p><code>' . esc_html( $state['loopback_error'] ) . '</code></p>' : '' );
			$result['actions']     = '<p><a href="' . esc_url( admin_url( 'admin.php?page=sfrec-settings' ) ) . '">' . esc_html__( 'Review SEO Flight Recorder settings', 'buildsbyluke-seo-flight-recorder' ) . '</a></p>';
		}

		return $result;
	}

	/**
	 * Test 2: any open critical findings?
	 *
	 * @return array
	 */
	public static function test_findings() {
		$counts = SFRec_Admin::open_counts();

		$result = array(
			'label'       => __( 'No unresolved critical SEO findings', 'buildsbyluke-seo-flight-recorder' ),
			'status'      => 'good',
			'badge'       => array(
				'label' => __( 'SEO', 'buildsbyluke-seo-flight-recorder' ),
				'color' => 'blue',
			),
			'description' => '<p>' . esc_html__( 'SEO Flight Recorder has no open critical findings for this site.', 'buildsbyluke-seo-flight-recorder' ) . '</p>',
			'actions'     => '',
			'test'        => 'sfrec_findings',
		);

		if ( $counts['critical'] > 0 ) {
			$result['status'] = 'recommended';
			/* translators: %d: number of critical findings. */
			$result['label']       = sprintf( _n( '%d critical SEO finding is unresolved', '%d critical SEO findings are unresolved', $counts['critical'], 'buildsbyluke-seo-flight-recorder' ), $counts['critical'] );
			$result['description'] = '<p>' . esc_html__( 'A critical finding means something is actively hurting this site\'s search visibility: a noindex, a broken sitemap, a blocking robots.txt, a vanished schema type, or an unreachable monitored page.', 'buildsbyluke-seo-flight-recorder' ) . '</p>';
			$result['actions']     = '<p><a href="' . esc_url( admin_url( 'admin.php?page=seo-flight-recorder' ) ) . '">' . esc_html__( 'Open the flight log', 'buildsbyluke-seo-flight-recorder' ) . '</a></p>';
		}

		return $result;
	}
}
