<?php
/**
 * Handles the admin status page.
 *
 * @package Northmill_Express_Order
 */

declare(strict_types=1);

namespace Flo_Checkout\Admin\Pages;

use Flo_Checkout\Gateways\Apple_Pay\Merchant_Validator;
use Flo_Checkout\Gateways\Shift4_APM\Debug;
use Flo_Checkout\Gateways\Shift4_APM\Enums\Health_Check_API;
use Flo_Checkout\Gateways\Shift4_APM\Health_Check;
use Flo_Checkout\Gateways\Swish\Swish_API;
use Flo_Checkout\Plugin_Upgrade;

defined( 'ABSPATH' ) || exit;

/**
 * Class Admin_Status_Page
 */
class Admin_Status_Page {

	/**
	 * Set up hooks for Shared.
	 *
	 * @param string $service_name_underscored The service name underscored.
	 *
	 * @return void
	 */
	public static function setup_shared_hooks( string $service_name_underscored ): void {
		add_filter(
			'northmill_online_shared_admin_status_tabs_' . $service_name_underscored,
			array( self::class, 'filter_status_page_tabs' )
		);
	}

	/**
	 * Filter the tabs on the status page.
	 *
	 * @param array $tabs The current tabs.
	 *
	 * @return array The updated tabs
	 */
	public static function filter_status_page_tabs( array $tabs ): array {
		$tabs['shift4-apm'] = array(
			'title'         => __( 'Shift4 APM', 'flo-checkout-plugin' ),
			'template_part' => 'shift4-apm',
			'view_params'   => function () {
				return array(
					'general'       => array(
						'hmac hash' => Debug::get_hmac_status(),
					),
					'apple_pay'     => array(
						'MID'           => Merchant_Validator::get_merchant_identifier(),
						'MID cert path' => Merchant_Validator::get_cer_file_path(),
					),
					'health_checks' => array(
						'payment_prod'      => Health_Check::get_status( Health_Check_API::PAYMENT, false ),
						'payment_int'       => Health_Check::get_status( Health_Check_API::PAYMENT, true ),
						'notification_prod' => Health_Check::get_status( Health_Check_API::NOTIFICATION, false ),
						'notification_int'  => Health_Check::get_status( Health_Check_API::NOTIFICATION, true ),
					),
				);
			},
		);
		$tabs['swish']      = array(
			'title'         => __( 'Swish', 'flo-checkout-plugin' ),
			'template_part' => 'swish',
			'view_params'   => function () {
				return array(
					'general' => Swish_API::get_debug_ssl_env(),
				);
			},
		);

		self::maybe_add_test_tab( $tabs );

		return $tabs;
	}

	/**
	 * Maybe add a test tab.
	 *
	 * @param array $tab The current sections.
	 *
	 * @phpcs:disable Squiz.PHP.NonExecutableCode.Unreachable
	 * @phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_print_r
	 * @phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar
	 * @phpcs:disable Squiz.PHP.CommentedOutCode.Found
	 * @noinspection PhpUnreachableStatementInspection
	 * @noinspection PhpUnusedParameterInspection
	 * @noinspection RedundantSuppression
	 */
	private static function maybe_add_test_tab( array &$tab ): void {
		return;

		ob_start();

		$updates = Plugin_Upgrade::get_upgrades( array() );
		$updater = $updates['2.2.0'];
		// $updater( false );

		$content     = ob_get_clean();
		$tab['test'] = array(
			'title'   => 'Test',
			'content' => $content,
		);
	}
	// @phpcs:enable
}
