<?php
/**
 * Support Widget — contextual help notices on plugin admin pages.
 *
 * Self-registering: instantiate once and all hooks are set up automatically.
 * Does NOT modify any existing functionality — purely additive.
 *
 * @package WahraRentalsBookings
 * @since   2.0.6
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class WAHRB_Support_Widget
 */
class WAHRB_Support_Widget {

	/**
	 * WordPress.org support forum URL.
	 *
	 * @var string
	 */
	private string $support_url = 'https://wordpress.org/support/plugin/wahra-rentals-bookings/';

	/**
	 * Documentation URL.
	 *
	 * @var string
	 */
	private string $docs_url = 'https://wahra.tn/docs/';

	/**
	 * Constructor — register hooks.
	 */
	public function __construct() {
		add_action( 'admin_notices', array( $this, 'maybe_show_support_notices' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_support_styles' ) );
	}

	/**
	 * Check whether the current screen belongs to our plugin.
	 *
	 * @return bool
	 */
	private function is_plugin_page(): bool {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return false;
		}

		// Match any of our admin page screen IDs.
		$plugin_screens = array(
			'wahrb-settings',
			'wahrb-bookings',
			'wahrb-wizard',
			'wahrb-calendar',
			'wahrb-analytics',
			'wahrb-vendor-settings',
			'toplevel_page_wahrb',
		);

		foreach ( $plugin_screens as $id_fragment ) {
			if ( false !== strpos( $screen->id, $id_fragment ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Conditionally display support notices.
	 */
	public function maybe_show_support_notices(): void {
		if ( ! $this->is_plugin_page() ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$notices = $this->detect_issues();

		if ( empty( $notices ) ) {
			return;
		}

		foreach ( $notices as $notice ) {
			printf(
				'<div class="notice notice-%1$s wahrb-support-notice is-dismissible"><p>%2$s</p></div>',
				esc_attr( $notice['type'] ),
				wp_kses_post( $notice['message'] )
			);
		}
	}

	/**
	 * Detect environment issues and return contextual help notices.
	 *
	 * @return array<int, array{type: string, message: string}>
	 */
	private function detect_issues(): array {
		$notices = array();

		// PHP version check.
		if ( version_compare( PHP_VERSION, '8.1', '<' ) ) {
			$notices[] = array(
				'type'    => 'warning',
				'message' => sprintf(
					/* translators: 1: Current PHP version, 2: Support forum URL */
					__( '<strong>Rentals & Bookings:</strong> Your server runs PHP %1$s. PHP 8.1+ is required for full functionality. <a href="%2$s" target="_blank">Get help →</a>', 'wahra-rentals-bookings' ),
					esc_html( PHP_VERSION ),
					esc_url( $this->support_url )
				),
			);
		}

		// WooCommerce missing or outdated.
		if ( ! class_exists( 'WooCommerce' ) ) {
			$notices[] = array(
				'type'    => 'error',
				'message' => sprintf(
					/* translators: %s: Support forum URL */
					__( '<strong>Rentals & Bookings:</strong> WooCommerce is required but not active. <a href="%s" target="_blank">Installation guide →</a>', 'wahra-rentals-bookings' ),
					esc_url( $this->docs_url . 'installation/' )
				),
			);
		} elseif ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '8.0', '<' ) ) {
			$notices[] = array(
				'type'    => 'warning',
				'message' => sprintf(
					/* translators: 1: Current WooCommerce version, 2: Documentation URL */
					__( '<strong>Rentals & Bookings:</strong> WooCommerce %1$s detected. Version 8.0+ is recommended. <a href="%2$s" target="_blank">Compatibility guide →</a>', 'wahra-rentals-bookings' ),
					esc_html( WC_VERSION ),
					esc_url( $this->docs_url . 'compatibility/' )
				),
			);
		}

		// Dokan mode detection — informational only.
		if ( ! wahrb_is_dokan_active() ) {
			$notices[] = array(
				'type'    => 'info',
				'message' => sprintf(
					/* translators: %s: Documentation URL */
					__( '<strong>Rentals & Bookings:</strong> Running in Solo Vendor mode (WooCommerce only). To enable multi-vendor features, install Dokan. <a href="%s" target="_blank">Solo vs Multi-Vendor guide →</a>', 'wahra-rentals-bookings' ),
					esc_url( $this->docs_url . 'solo-vs-dokan/' )
				),
			);
		}

		return $notices;
	}

	/**
	 * Enqueue minimal inline CSS for support notices — only on plugin pages.
	 */
	public function enqueue_support_styles(): void {
		if ( ! $this->is_plugin_page() ) {
			return;
		}

		$css = '
			.wahrb-support-notice a {
				text-decoration: underline;
				font-weight: 600;
			}
			.wahrb-support-notice strong {
				color: #1d2327;
			}
		';

		wp_register_style( 'wahrb-support-widget', false, array(), WAHRB_VERSION );
		wp_enqueue_style( 'wahrb-support-widget' );
		wp_add_inline_style( 'wahrb-support-widget', $css );
	}
}
