<?php
/**
 * Admin-specific functionality.
 */

namespace RawTypeOE\Free\Admin;

use RawTypeOE\Free\Plugin;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles all admin-facing functionality.
 */
class Admin {

	/** Register admin hooks. */
	public function register_hooks() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
		add_filter( 'plugin_action_links_' . RAWTYPE_OE_PLUGIN_BASENAME, array( $this, 'add_action_links' ) );
	}

	/**
	 * Enqueue admin assets (React app from build/).
	 *
	 * @param string $hook The current admin page hook.
	 */
	public function enqueue_assets( $hook ) {
		if ( 'woocommerce_page_rawtype-order-exporter-woocommerce' !== $hook ) {
			return;
		}

		// Hide WordPress admin notices on the plugin page.
		remove_all_actions( 'admin_notices' );
		remove_all_actions( 'all_admin_notices' );

		$asset_file = RAWTYPE_OE_PLUGIN_DIR . 'build/admin/index.asset.php';

		if ( ! file_exists( $asset_file ) ) {
			return;
		}

		$asset        = require $asset_file;
		$dependencies = isset( $asset['dependencies'] ) && is_array( $asset['dependencies'] ) ? $asset['dependencies'] : array();
		$version      = isset( $asset['version'] ) ? $asset['version'] : RAWTYPE_OE_VERSION;

		wp_enqueue_style(
			'rawtype-oe-admin',
			RAWTYPE_OE_PLUGIN_URL . 'build/admin/index.css',
			array(),
			$version
		);

		wp_enqueue_script(
			'rawtype-oe-admin',
			RAWTYPE_OE_PLUGIN_URL . 'build/admin/index.js',
			$dependencies,
			$version,
			true
		);

		wp_set_script_translations(
			'rawtype-oe-admin',
			'rawtype-order-exporter-woocommerce',
			RAWTYPE_OE_PLUGIN_DIR . 'languages'
		);

		wp_localize_script(
			'rawtype-oe-admin',
			'rawtypeOEAdmin',
			array(
				'restUrl'   => esc_url_raw( rest_url() ),
				'nonce'     => wp_create_nonce( 'wp_rest' ),
				'pluginUrl' => esc_url_raw( RAWTYPE_OE_PLUGIN_URL ),
				'version'   => RAWTYPE_OE_VERSION,
				'isPro'     => (bool) apply_filters( 'rawtype_oe_is_pro', false ),
			)
		);
	}

	/** Add plugin submenu under WooCommerce. */
	public function add_admin_menu() {
		add_submenu_page(
			'woocommerce',
			esc_html__( 'Export Excel', 'rawtype-order-exporter-woocommerce' ),
			esc_html__( 'Export Excel', 'rawtype-order-exporter-woocommerce' ),
			'manage_woocommerce',
			'rawtype-order-exporter-woocommerce',
			array( $this, 'render_settings_page' )
		);
	}

	/**
	 * Add action links to plugin list.
	 *
	 * @param array $links Existing action links.
	 * @return array Modified action links.
	 */
	public function add_action_links( $links ) {
		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			admin_url( 'admin.php?page=rawtype-order-exporter-woocommerce' ),
			esc_html__( 'Settings', 'rawtype-order-exporter-woocommerce' )
		);
		array_unshift( $links, $settings_link );

		if ( ! Plugin::is_pro() ) {
			$upgrade_link = sprintf(
				'<a href="%s" style="color:#FF5722;font-weight:bold;">%s</a>',
				esc_url( 'https://rawtype.dev/rawtype-order-exporter-woocommerce' ),
				esc_html__( 'Upgrade to Pro', 'rawtype-order-exporter-woocommerce' )
			);
			$links[]      = $upgrade_link;
		}

		return $links;
	}

	/** Render the settings page (React mount point). */
	public function render_settings_page() {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			return;
		}

		include RAWTYPE_OE_PLUGIN_DIR . 'admin/partials/admin-display.php';
	}
}
