<?php
/**
 * Class Noesia Cs System Status Service.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_System_Status_Service {
	/**
	 * Handles the related plugin operation.
	 */
	public function get_status() {
		$settings = Noesia_CS_Settings::get_all();
		$status   = array(
			'plugin_version'        => NOESIA_CS_VERSION,
			'wordpress_version'     => get_bloginfo( 'version' ),
			'php_version'           => PHP_VERSION,
			'woocommerce_active'    => class_exists( 'WooCommerce' ),
			'woocommerce_version'   => defined( 'WC_VERSION' ) ? WC_VERSION : '',
			'hpos_declared'         => true,
			'hpos_enabled'          => $this->is_hpos_enabled(),
			'cron_next_run'         => wp_next_scheduled( Noesia_CS_Reorder_Service::CRON_HOOK ),
			'care_endpoint'         => home_url( user_trailingslashit( 'noesia-care/{secure-token}' ) ),
			'optout_endpoint'       => home_url( user_trailingslashit( 'noesia-reorder-opt-out/{secure-token}' ) ),
			'care_action_endpoint'  => home_url( user_trailingslashit( 'noesia-care-action/{secure-token}/{action}' ) ),
			'impact_shortcode'      => '[noesia_impact_map]',
			'impact_alias'          => '[noesia_cs_impact_map]',
			'impact_snapshot'       => get_option( Noesia_CS_Impact_Map_Service::SNAPSHOT_OPTION, array() ),
			'public_modes'          => array(
				'care_page'             => $settings['enable_care_page'],
				'privacy_noindex'       => $settings['privacy_noindex'],
				'require_login'         => $settings['require_login_care_page'],
				'impact_map'            => $settings['enable_impact_map_shortcode'],
			),
			'integrations' => array(
				'thankyou'      => $settings['thankyou_integration'],
				'my_account'    => $settings['my_account_integration'],
				'email'         => $settings['email_integration'],
				'order_admin'   => $settings['order_admin_metabox'],
			),
			'email_modes'       => array(
				'passport_email'     => $settings['passport_email_enabled'],
				'reorder_email'      => $settings['reorder_email_enabled'],
				'reorder_reminders'  => $settings['reorder_reminders_enabled'],
			),
		);

		$status['checks'] = $this->build_checks( $status );
		return $status;
	}

	/**
	 * Handles the related plugin operation.
	 */
	private function is_hpos_enabled() {
		if ( ! class_exists( '\\Automattic\\WooCommerce\\Utilities\\OrderUtil' ) ) {
			return false;
		}
		return (bool) \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $status Status.
	 */
	private function build_checks( $status ) {
		$checks    = array();
		$settings  = Noesia_CS_Settings::get_all();

		if ( 'yes' === $settings['wc_status_check'] ) {
			$checks[] = array(
				'label'    => __( 'WooCommerce', 'noesia-commerce-suite' ),
				'status'   => $status['woocommerce_active'] ? 'ok' : 'warning',
				'message'  => $status['woocommerce_active'] ? sprintf( /* translators: %s: WooCommerce version. */ __( 'Active %s', 'noesia-commerce-suite' ), $status['woocommerce_version'] ) : __( 'WooCommerce is required for live commerce features.', 'noesia-commerce-suite' ),
			);
		}

		if ( 'yes' === $settings['hpos_status_check'] ) {
			$checks[] = array(
				'label'    => __( 'HPOS Compatibility', 'noesia-commerce-suite' ),
				'status'   => 'ok',
				'message'  => $status['hpos_enabled'] ? __( 'Declared and WooCommerce HPOS is enabled.', 'noesia-commerce-suite' ) : __( 'Declared. HPOS may be disabled by the store.', 'noesia-commerce-suite' ),
			);
		}

		$checks[] = array(
			'label'    => __( 'Reorder Reminder Schedule', 'noesia-commerce-suite' ),
			'status'   => $status['cron_next_run'] ? 'ok' : 'warning',
			'message'  => $status['cron_next_run'] ? sprintf( /* translators: %s: next cron date/time. */ __( 'Next run: %s', 'noesia-commerce-suite' ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $status['cron_next_run'] ) ) ) : __( 'No automatic reorder reminder event was found. Reactivation or schedule repair may be needed.', 'noesia-commerce-suite' ),
		);

		$areas = isset( $status['impact_snapshot']['areas'] ) && is_array( $status['impact_snapshot']['areas'] ) ? count( $status['impact_snapshot']['areas'] ) : 0;
		$checks[] = array(
			'label'    => __( 'Impact Map Snapshot', 'noesia-commerce-suite' ),
			'status'   => $areas > 0 ? 'ok' : 'info',
			'message'  => $areas > 0 ? sprintf( /* translators: %d: public areas count. */ __( '%d privacy-safe public areas cached.', 'noesia-commerce-suite' ), absint( $areas ) ) : __( 'No cached public areas yet. Refresh the snapshot from the Impact Map tab after real orders exist.', 'noesia-commerce-suite' ),
		);

		$checks[] = array(
			'label'    => __( 'Public Privacy Mode', 'noesia-commerce-suite' ),
			'status'   => 'yes' === $status['public_modes']['privacy_noindex'] ? 'ok' : 'warning',
			'message'  => 'yes' === $status['public_modes']['privacy_noindex'] ? __( 'Care pages are configured as noindex.', 'noesia-commerce-suite' ) : __( 'Care pages are not configured as noindex.', 'noesia-commerce-suite' ),
		);

		return $checks;
	}
}
