<?php
defined( 'ABSPATH' ) || exit;

/**
 * Settings page and admin asset enqueuing.
 *
 * Settings page: WooCommerce > Tax Exemption
 */
class Rankd_Tx_Admin {

	public static function init() {
		// Free tier has no review-queue page for Freemius to anchor on, so
		// it needs this dedicated redirect page. Pro tier's Freemius
		// instance is configured (main plugin file) to anchor directly on
		// the review queue's own slug instead — registering this page
		// there too would recreate the exact duplicate-menu-item problem
		// this was built to solve, so it's skipped entirely in Pro.
		if ( ! file_exists( RANKD_TX_PLUGIN_DIR . 'includes/pro/class-rankd-tx-review-queue.php' ) ) {
			add_action( 'admin_menu', array( __CLASS__, 'register_freemius_page' ), 100 );
		}
		add_filter( 'woocommerce_get_settings_pages', array( __CLASS__, 'add_settings_page' ) );
		add_action( 'admin_enqueue_scripts',           array( __CLASS__, 'enqueue_assets' ) );
	}

	// -------------------------------------------------------------------------
	// Settings page
	// -------------------------------------------------------------------------

	public static function add_settings_page( $pages ) {
		require_once RANKD_TX_PLUGIN_DIR . 'includes/admin/class-rankd-tx-settings-page.php';
		$pages[] = new Rankd_Tx_Settings_Page();
		return $pages;
	}

	// -------------------------------------------------------------------------
	// Admin assets
	// -------------------------------------------------------------------------

	/**
	 * Registers the WooCommerce submenu page Freemius anchors to.
	 *
	 * FREE TIER ONLY — see the skip condition in init(). Freemius builds
	 * its page URLs from this menu slug:
	 *   admin.php?page=rankd-tx-settings           (opt-in, activation)
	 *   admin.php?page=rankd-tx-settings-account   (account)
	 *   admin.php?page=rankd-tx-settings-pricing   (upgrade/checkout)
	 *
	 * All of these resolve as WooCommerce submenu pages — WooCommerce's
	 * parent file is admin.php, so the URL shape is identical to the
	 * top-level case.
	 */
	public static function register_freemius_page() {
		add_submenu_page(
			'woocommerce',
			__( 'Tax Exemption Settings', 'rankd-tax-exemption-certificates-for-woocommerce' ),
			__( 'Tax Exempt Settings', 'rankd-tax-exemption-certificates-for-woocommerce' ),
			'manage_woocommerce',
			'rankd-tx-settings',
			array( __CLASS__, 'render_settings_redirect' )
		);
	}

	/**
	 * Callback for the anchor page.
	 *
	 * During activation mode Freemius replaces this callback entirely
	 * (override_submenu_action() removes the page-hook action and attaches
	 * its own _connect_page_render), so the guard below is belt-and-
	 * suspenders for fs_action requests routed through this slug. On a
	 * normal page view, redirect to where the plugin's settings actually
	 * live.
	 */
	public static function render_settings_redirect() {
		if ( function_exists( 'rankd_tx_fs' ) ) {
			$fs = rankd_tx_fs();
			if ( $fs->is_activation_mode() || isset( $_GET['fs_action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only routing check, no data processed.
				return;
			}
		}
		wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=rankd-tx-settings' ) );
		exit;
	}


	public static function enqueue_assets( $hook ) {
		// Load on user profile, our settings/review pages, and the dashboard
		// (the Pro widget's styles live in admin.css).
		$allowed_hooks = array(
			'index.php',
			'user-edit.php',
			'profile.php',
			'users.php', // Status badges in the Tax Exemption column.
			'woocommerce_page_wc-settings',
			'woocommerce_page_rankd-tx-review-queue',
		);

		if ( ! in_array( $hook, $allowed_hooks, true ) ) {
			return;
		}

		wp_enqueue_style(
			'rankd-tx-admin',
			RANKD_TX_PLUGIN_URL . 'assets/css/admin.css',
			array(),
			RANKD_TX_VERSION
		);

		// Pro-only scripts (Select2 user search, review queue JS) are
		// enqueued by Rankd_Tx_Pro_Admin, which only exists in the Pro build.
	}

	// The Nginx notice (+ dismiss/show handlers), scheduled-check health
	// notice, Run Now handler, Select2 user search, and nonce refresh
	// endpoint all belong to the Pro certificate workflow and live in
	// Rankd_Tx_Pro_Admin (includes/pro/), which is stripped from the free
	// build and initialised from Rankd_Tx_Pro::init().
}
