<?php
/**
 * Theme recommendation notice shown when FoodOrd Theme is inactive.
 *
 * @package FoodOrd
 */

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

/**
 * Class Foodord_Theme_Recommendation_Notice
 */
class Foodord_Theme_Recommendation_Notice {
	/**
	 * User meta key that stores dismissal state.
	 */
	private const DISMISS_META_KEY = 'foodord_theme_recommendation_notice_dismissed';

	/**
	 * Query arg used for manual dismiss action.
	 */
	private const DISMISS_QUERY_ARG = 'foodord_dismiss_theme_notice';

	/**
	 * Register hooks.
	 *
	 * @return void
	 */
	public function init(): void {
		add_action( 'admin_init', array( $this, 'handle_dismiss' ) );
		add_action( 'admin_notices', array( $this, 'render_notice' ) );
	}

	/**
	 * Persist dismissal when the user clicks the dismiss action.
	 *
	 * @return void
	 */
	public function handle_dismiss(): void {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! isset( $_GET[ self::DISMISS_QUERY_ARG ] ) ) {
			return;
		}

		$nonce = isset( $_GET['_foodord_notice_nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['_foodord_notice_nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'foodord_dismiss_theme_notice' ) ) {
			return;
		}

		$user_id = get_current_user_id();
		if ( $user_id > 0 ) {
			update_user_meta( $user_id, self::DISMISS_META_KEY, '1' );
		}

		$redirect_url = remove_query_arg(
			array(
				self::DISMISS_QUERY_ARG,
				'_foodord_notice_nonce',
			)
		);

		wp_safe_redirect( $redirect_url );
		exit;
	}

	/**
	 * Render the recommendation notice.
	 *
	 * @return void
	 */
	public function render_notice(): void {
		if ( ! $this->should_render_notice() ) {
			return;
		}

		$theme_status    = function_exists( 'foodord_get_theme_admin_status' ) ? foodord_get_theme_admin_status() : 'missing';
		$theme_installed = 'installed' === $theme_status;
		$action_url      = $theme_installed && function_exists( 'foodord_get_theme_activation_url' )
			? foodord_get_theme_activation_url()
			: ( function_exists( 'foodord_get_theme_download_url' ) ? foodord_get_theme_download_url() : admin_url( 'themes.php' ) );
		$action_label    = $theme_installed ? __( 'Activate FoodOrd Theme', 'foodord' ) : __( 'Download FoodOrd Theme', 'foodord' );
		$logo_url        = plugin_dir_url( __FILE__ ) . 'images/notice-logo.png';
		$dismiss_url  = add_query_arg(
			array(
				self::DISMISS_QUERY_ARG => '1',
			)
		);
		$dismiss_url  = wp_nonce_url( $dismiss_url, 'foodord_dismiss_theme_notice', '_foodord_notice_nonce' );
		?>
		<div class="notice notice-info foodord-theme-notice">
			<div class="foodord-theme-notice__logo-wrap" aria-hidden="true">
				<img class="foodord-theme-notice__logo" src="<?php echo esc_url( $logo_url ); ?>" alt="" />
			</div>
			<div class="foodord-theme-notice__content">
				<p class="foodord-theme-notice__description"><?php echo esc_html__( 'Advanced frontend components such as product modals, location flows, cart UI, and ordering layouts are officially optimized for FoodOrd Theme.', 'foodord' ); ?></p>
				<p class="foodord-theme-notice__actions">
					<a class="button button-primary" href="<?php echo esc_url( $action_url ); ?>"<?php if ( ! $theme_installed ) : ?> target="_blank" rel="noopener noreferrer"<?php endif; ?>><?php echo esc_html( $action_label ); ?></a>
					<a class="foodord-theme-notice__dismiss" href="<?php echo esc_url( $dismiss_url ); ?>"><?php echo esc_html__( 'Dismiss', 'foodord' ); ?></a>
				</p>
			</div>
		</div>
		<?php
	}

	/**
	 * Determine whether the notice should be visible.
	 *
	 * @return bool
	 */
	private function should_render_notice(): bool {
		if ( ! is_admin() || wp_doing_ajax() || ! current_user_can( 'manage_options' ) ) {
			return false;
		}

		if ( defined( 'FOODORD_THEME_ACTIVE' ) && FOODORD_THEME_ACTIVE ) {
			return false;
		}

		$user_id = get_current_user_id();
		if ( $user_id > 0 && '1' === get_user_meta( $user_id, self::DISMISS_META_KEY, true ) ) {
			return false;
		}

		return $this->is_supported_screen();
	}

	/**
	 * Limit the notice to relevant admin screens.
	 *
	 * @return bool
	 */
	private function is_supported_screen(): bool {
		if ( ! function_exists( 'get_current_screen' ) ) {
			return true;
		}

		$screen = get_current_screen();
		if ( ! $screen instanceof WP_Screen ) {
			return true;
		}

		$screen_id   = (string) $screen->id;
		$screen_base = (string) $screen->base;

		if ( false !== strpos( $screen_id, 'foodord' ) ) {
			return true;
		}

		return in_array(
			$screen_base,
			array(
				'dashboard',
				'plugins',
				'themes',
				'appearance_page',
			),
			true
		);
	}
}
