<?php
/**
 * Admin Pages for Fair Payments Connector
 *
 * @package FairPaymentsConnector
 */

namespace FairPaymentsConnector\Admin;

use FairEventsShared\Money;
use FairPaymentsConnector\Payment\MolliePaymentHandler;

defined( 'WPINC' ) || die;

/**
 * Admin Pages class for registering admin menu pages
 */
class AdminPages {
	/**
	 * Initialize admin pages
	 *
	 * @return void
	 */
	public function init() {
		add_action( 'admin_menu', array( $this, 'register_admin_pages' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
		add_action( 'admin_notices', array( $this, 'render_test_mode_notice' ) );
		add_filter( 'plugin_action_links_fair-payments-connector/fair-payments-connector.php', array( $this, 'add_plugin_action_links' ) );
	}

	/**
	 * Register admin menu pages
	 *
	 * @return void
	 */
	public function register_admin_pages() {
		// Main transactions page.
		add_menu_page(
			__( 'Fair Payments Connector', 'fair-payments-connector' ),
			__( 'Fair Payments Connector', 'fair-payments-connector' ),
			'manage_options',
			'fair-payments-connector-transactions',
			array( $this, 'render_transactions_page' ),
			'dashicons-money-alt',
			'56'
		);

		// Transactions submenu (duplicate to rename main menu item).
		add_submenu_page(
			'fair-payments-connector-transactions',
			__( 'Transactions', 'fair-payments-connector' ),
			__( 'Transactions', 'fair-payments-connector' ),
			'manage_options',
			'fair-payments-connector-transactions'
		);

		// Hidden transaction detail page.
		$transaction_hookname = add_submenu_page(
			'',
			__( 'Transaction Detail', 'fair-payments-connector' ),
			__( 'Transaction Detail', 'fair-payments-connector' ),
			'manage_options',
			'fair-payments-connector-transaction',
			array( $this, 'render_transaction_page' )
		);

		// Set page title for hidden page to prevent strip_tags() deprecation warning.
		$this->set_hidden_page_title( $transaction_hookname, __( 'Transaction Detail', 'fair-payments-connector' ) );

		// Fee Dashboard submenu (position 10 — gaps reserved for experimental plugin).
		add_submenu_page(
			'fair-payments-connector-transactions',
			__( 'Fee Dashboard', 'fair-payments-connector' ),
			__( 'Fee Dashboard', 'fair-payments-connector' ),
			'manage_options',
			'fair-payments-connector-fee-dashboard',
			array( $this, 'render_fee_dashboard_page' ),
			10
		);

		// Settings submenu (position 20 — gaps reserved for experimental plugin).
		add_submenu_page(
			'fair-payments-connector-transactions',
			__( 'Settings', 'fair-payments-connector' ),
			__( 'Settings', 'fair-payments-connector' ),
			'manage_options',
			'fair-payments-connector-settings',
			array( $this, 'render_settings_page' ),
			20
		);
	}

	/**
	 * Enqueue admin scripts
	 *
	 * @param string $hook Current admin page hook.
	 * @return void
	 */
	public function enqueue_admin_scripts( $hook ) {
		// Transactions page.
		if ( 'toplevel_page_fair-payments-connector-transactions' === $hook ) {
			$this->enqueue_admin_page_script( 'transactions' );
			wp_localize_script(
				'fair-payments-connector-transactions',
				'fairPaymentTransactions',
				array(
					'organizationId' => get_option( 'fair_payment_organization_id', '' ),
					'testMode'       => 'test' === get_option( 'fair_payment_mode', 'test' ),
				)
			);
			return;
		}

		// Settings page.
		if ( false !== strpos( $hook, 'fair-payments-connector-settings' ) ) {
			$this->enqueue_admin_page_script( 'settings' );
			return;
		}

		// Fee Dashboard page.
		if ( false !== strpos( $hook, 'fair-payments-connector-fee-dashboard' ) ) {
			$this->enqueue_admin_page_script( 'fee-dashboard' );
			wp_localize_script(
				'fair-payments-connector-fee-dashboard',
				'fairPaymentsConnector',
				array(
					'currency' => Money::site_currency(),
				)
			);
			return;
		}

		// Transaction detail page.
		if ( 'admin_page_fair-payments-connector-transaction' === $hook ) {
			$this->enqueue_admin_page_script( 'transaction' );
			wp_localize_script(
				'fair-payments-connector-transaction',
				'fairPaymentTransactions',
				array(
					'organizationId' => get_option( 'fair_payment_organization_id', '' ),
				)
			);
			return;
		}
	}

	/**
	 * Render a non-dismissible warning notice on all admin pages when in test mode.
	 *
	 * @return void
	 */
	public function render_test_mode_notice() {
		if ( 'test' !== get_option( 'fair_payment_mode', 'test' ) ) {
			return;
		}

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

		$settings_url = add_query_arg( 'page', 'fair-payments-connector-settings', admin_url( 'admin.php' ) );

		if ( MolliePaymentHandler::is_configured() ) {
			/* translators: %s: link to settings page */
			$message = __( 'Fair Payment is in <strong>Test mode</strong> — no real payments are being processed. <a href="%s">Switch to Live mode</a>.', 'fair-payments-connector' );
		} else {
			/* translators: %s: link to settings page */
			$message = __( 'Fair Payment is <strong>not set up</strong> — no payments can be processed yet. <a href="%s">Set up Mollie</a>.', 'fair-payments-connector' );
		}
		$notice_message = sprintf(
			wp_kses(
				$message,
				array(
					'strong' => array(),
					'a'      => array( 'href' => array() ),
				)
			),
			esc_url( $settings_url )
		);

		wp_admin_notice(
			$notice_message,
			array(
				'type'        => 'warning',
				'dismissible' => false,
			)
		);
	}

	/**
	 * Add action links on the plugins list page.
	 *
	 * Shows a "Set up" link when Mollie is not yet configured (no OAuth connection),
	 * so admins are guided to the settings page.
	 *
	 * @param string[] $links Existing action links.
	 * @return string[] Modified action links.
	 */
	public function add_plugin_action_links( $links ) {
		if ( ! MolliePaymentHandler::is_configured() ) {
			$settings_url = add_query_arg( 'page', 'fair-payments-connector-settings', admin_url( 'admin.php' ) );
			$setup_link   = '<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Set up', 'fair-payments-connector' ) . '</a>';
			array_unshift( $links, $setup_link );
		}

		return $links;
	}

	/**
	 * Set the page title for a hidden admin page.
	 *
	 * Hidden pages (registered with empty parent slug) are not in the submenu array,
	 * so WordPress cannot find their title. This causes $title to be null when
	 * admin-header.php calls strip_tags(), triggering a PHP 8.1+ deprecation warning.
	 *
	 * @param string $hookname The page hook name returned by add_submenu_page().
	 * @param string $page_title The title to set.
	 * @return void
	 */
	private function set_hidden_page_title( $hookname, $page_title ) {
		add_action(
			'load-' . $hookname,
			static function () use ( $page_title ) {
				global $title;
				// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
				$title = $page_title;
			}
		);
	}

	/**
	 * Enqueue script for an admin page
	 *
	 * @param string $page Page name (transactions, transaction, settings, fee-dashboard).
	 * @return void
	 */
	private function enqueue_admin_page_script( $page ) {
		$asset_file_path = FAIR_PAYMENTS_CONNECTOR_PLUGIN_DIR . 'build/admin/' . $page . '/index.asset.php';

		if ( ! file_exists( $asset_file_path ) ) {
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
				error_log( 'Fair Payments Connector: Asset file not found at ' . $asset_file_path );
			}
			return;
		}

		$asset_file = include $asset_file_path;

		wp_enqueue_script(
			'fair-payments-connector-' . $page,
			FAIR_PAYMENTS_CONNECTOR_PLUGIN_URL . 'build/admin/' . $page . '/index.js',
			$asset_file['dependencies'],
			$asset_file['version'],
			true
		);

		wp_enqueue_style( 'wp-components' );

		// Enqueue the page's stylesheet when one was emitted by the build.
		// wp-scripts writes style-index.css when index.js imports a stylesheet.
		$style_file_path = FAIR_PAYMENTS_CONNECTOR_PLUGIN_DIR . 'build/admin/' . $page . '/style-index.css';

		if ( file_exists( $style_file_path ) ) {
			wp_enqueue_style(
				'fair-payments-connector-' . $page,
				FAIR_PAYMENTS_CONNECTOR_PLUGIN_URL . 'build/admin/' . $page . '/style-index.css',
				array( 'wp-components' ),
				$asset_file['version']
			);
		}
	}

	/**
	 * Render settings page
	 *
	 * @return void
	 */
	public function render_settings_page() {
		?>
		<div id="fair-payments-connector-settings-root"></div>
		<?php
	}

	/**
	 * Render transaction detail page
	 *
	 * @return void
	 */
	public function render_transaction_page() {
		?>
		<div id="fair-payments-connector-transaction-root"></div>
		<?php
	}

	/**
	 * Render transactions page
	 *
	 * @return void
	 */
	public function render_transactions_page() {
		?>
		<div id="fair-payments-connector-transactions-root"></div>
		<?php
	}

	/**
	 * Render fee dashboard page
	 *
	 * @return void
	 */
	public function render_fee_dashboard_page() {
		?>
		<div id="fair-payments-connector-fee-dashboard-root"></div>
		<?php
	}
}
