<?php
/**
 * Deactivation Feedback Hooks
 *
 * Registers admin hooks for intercepting plugin deactivation on free plan
 * and wiring feedback submission endpoints.
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync/includes/common/hooks
 * @author     miniOrange
 */

declare(strict_types=1);

namespace MOMCS_Sync\Includes\Common\Hooks;

use MOMCS_Sync\Includes\Common\Constants\Plugin_Constants;
use MOMCS_Sync\Includes\Common\Controllers\Deactivation_Feedback_Controller;
use MOMCS_Sync\Includes\Common\Utils\Utils;
use MOMCS_Sync\Views\Src\Common\Components\Deactivation_Feedback_View;

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

/**
 * Class Deactivation_Feedback_Hooks
 *
 * Wires hooks for the free plan deactivation feedback flow.
 */
class Deactivation_Feedback_Hooks {
	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->register_hooks();
	}

	/**
	 * Register admin hooks for feedback modal and AJAX.
	 *
	 * @return void
	 */
	private function register_hooks(): void {
		if ( Utils::is_premium_plan() ) {
			return;
		}

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_footer', array( $this, 'render_modal' ) );
		add_action(
			'wp_ajax_' . Plugin_Constants::AJAX_DEACTIVATION_FEEDBACK,
			array( Deactivation_Feedback_Controller::class, 'handle_submission' )
		);
	}

	/**
	 * Enqueue deactivation feedback assets on plugins screen.
	 *
	 * @param string $hook_suffix Current admin page hook.
	 * @return void
	 */
	public function enqueue_assets( string $hook_suffix ): void {
		if ( Utils::is_premium_plan() || 'plugins.php' !== $hook_suffix ) {
			return;
		}

		$version = defined( 'MOMCS_VERSION' ) ? MOMCS_VERSION : '1.0.0';

		wp_register_script(
			'momcs-deactivation-feedback',
			MOMCS_PLUGIN_URL . 'assets/js/deactivation-feedback.js',
			array( 'jquery' ),
			$version,
			true
		);

		wp_localize_script(
			'momcs-deactivation-feedback',
			'momcsDeactivationFeedback',
			array(
				'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
				'nonce'          => wp_create_nonce( Plugin_Constants::NONCE_MAIN ),
				'action'         => Plugin_Constants::AJAX_DEACTIVATION_FEEDBACK,
				'pluginBasename' => MOMCS_PLUGIN_BASENAME,
				'strings'        => array(
					'selectReason'   => __( 'Please select a reason to continue.', 'multi-channel-for-woocommerce' ),
					'invalidEmail'   => __( 'Please enter a valid email address.', 'multi-channel-for-woocommerce' ),
					'genericError'   => __( 'Something went wrong. Please try again.', 'multi-channel-for-woocommerce' ),
					'successMessage' => __( 'Thanks for the feedback! Redirecting to deactivate...', 'multi-channel-for-woocommerce' ),
				),
			)
		);

		wp_enqueue_script( 'momcs-deactivation-feedback' );
	}

	/**
	 * Render the deactivation feedback modal markup.
	 *
	 * @return void
	 */
	public function render_modal(): void {
		if ( Utils::is_premium_plan() || ! $this->is_plugins_screen() ) {
			return;
		}
		( new Deactivation_Feedback_View() )->render();
	}

	/**
	 * Check if current screen is plugins list.
	 *
	 * @return bool
	 */
	private function is_plugins_screen(): bool {
		if ( ! function_exists( 'get_current_screen' ) ) {
			return false;
		}

		$screen = get_current_screen();
		if ( ! $screen ) {
			return false;
		}

		return in_array( $screen->id, array( 'plugins', 'plugins-network' ), true );
	}
}
