<?php
/**
 * Dashboard Hooks – wires WordPress hooks to dashboard controller endpoints.
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/hooks
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Hooks;

use MOMCS_Sync\Includes\Common\Controllers\Dashboard_Controller;
use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants;
use MOMCS_Sync\Views\Src\Common\Components\Contact_Us_View;

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

/**
 * Class Dashboard_Hooks
 *
 * Registers admin menu pages and localizes admin scripts for Dashboard pages.
 */
class Dashboard_Hooks {
	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register admin-related hooks.
	 */
	private function register_hooks(): void {
		// Admin menu pages via instance to avoid static call to non-static method.
		add_action(
			'admin_menu',
			function () {
				( new Dashboard_Controller() )->register_menu_pages();
			}
		);

		// Save screen option for Products per page.
		add_filter( 'set_screen_option_momcs_products_per_page', array( $this, 'save_screen_option' ), 10, 3 );

		// Enqueue dashboard/product-sync specific assets via controller.
		add_action(
			'admin_enqueue_scripts',
			function ( $hook_suffix ) {
				( new Dashboard_Controller() )->enqueue_scripts( $hook_suffix );
			}
		);

		// Localize params.
		add_action( 'admin_enqueue_scripts', array( $this, 'maybe_localize_scripts' ) );
		add_action(
			'admin_footer',
			function () {
				$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
				// Any plugin admin screen (Orders, Account, etc.) — match class-enqueue-scripts.php.
				if ( ! $screen || false === strpos( $screen->id, 'momcs-sync' ) ) {
					return;
				}

				// Prepare localized data for the Contact Us script.
				$current_user   = wp_get_current_user();
				$first_name     = $current_user && isset( $current_user->user_firstname ) ? $current_user->user_firstname : '';
				$last_name      = $current_user && isset( $current_user->user_lastname ) ? $current_user->user_lastname : '';
				$plugin_name    = 'Multi-channel for WooCommerce';
				$plugin_version = defined( 'MOMCS_VERSION' ) ? MOMCS_VERSION : '';
				$site_url       = site_url();

				$vars = array(
					'contact_us_url' => apply_filters( 'momcs_contact_us_remote_url', Plugin_Constants::CONTACT_US_REMOTE_URL ),
					'support_email'  => Plugin_Constants::SUPPORT_EMAIL,
					'first_name'     => esc_js( $first_name ),
					'last_name'      => esc_js( $last_name ),
					'plugin_name'    => esc_js( $plugin_name ),
					'version'        => esc_js( $plugin_version ),
					'site_url'       => esc_js( $site_url ),
				);

				// Register, localize, and enqueue the Contact Us script.
				wp_register_script(
					'momcs-contact-us',
					MOMCS_PLUGIN_URL . 'assets/js/contact-us.js',
					array( 'jquery' ),
					defined( 'MOMCS_VERSION' ) ? MOMCS_VERSION : '1.0.0',
					true
				);

				wp_localize_script( 'momcs-contact-us', 'momcsContactUs', $vars );
				wp_enqueue_script( 'momcs-contact-us' );

				// Render the Contact Us widget markup (no inline scripts).
				( new Contact_Us_View() )->render();
			}
		);
	}

	/**
	 * Localize common parameters for admin JS on our plugin screens.
	 *
	 * @param string $hook_suffix Current admin page hook.
	 */
	public function maybe_localize_scripts( string $hook_suffix ): void {
		// Only localize on our plugin screens to avoid polluting other admin pages.
		if ( false === strpos( $hook_suffix, 'momcs-sync' ) ) {
			return;
		}

		wp_localize_script(
			'momcs-sync-admin',
			'momcs_sync_params',
			array(
				'ajaxurl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( Plugin_Constants::NONCE_MAIN ),
			)
		);
	}

	/**
	 * Save screen option value for Products per page.
	 *
	 * @param mixed  $status The value to save instead of the option value.
	 * @param string $option The option name.
	 * @param int    $value  The option value.
	 * @return int|mixed The value to save.
	 */
	public function save_screen_option( $status, string $option, $value ) {
		if ( 'momcs_products_per_page' === $option ) {
			return absint( $value );
		}

		return $status;
	}
}
