<?php
/**
 * Deactivation Feedback
 *
 * Shows a feedback modal when the user deactivates the plugin.
 *
 * @package Quicker
 */

namespace Quicker\Core\Admin;

defined( 'ABSPATH' ) || exit;

use Quicker\Utils\Singleton;

class DeactivationFeedback {

	use Singleton;

	/**
	 * Plugin basename.
	 *
	 * @var string
	 */
	private $plugin_basename;

	/**
	 * API endpoint to receive feedback.
	 *
	 * @var string
	 */
	private $api_url = 'https://wpbens.com/wp-json/bens/v1/deactivation-feedback';

	/**
	 * Initialize hooks.
	 *
	 * @return void
	 */
	public function init() {
		$this->plugin_basename = \Quicker::plugins_basename();

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'admin_footer', array( $this, 'render_modal' ) );
		add_action( 'wp_ajax_quicker_deactivation_feedback', array( $this, 'handle_feedback' ) );
	}

	/**
	 * Only load on the plugins page.
	 *
	 * @return bool
	 */
	private function is_plugins_page() {
		$screen = get_current_screen();
		return $screen && 'plugins' === $screen->id;
	}

	/**
	 * Enqueue modal assets on plugins page only.
	 *
	 * @return void
	 */
	public function enqueue_assets() {
		if ( ! $this->is_plugins_page() ) {
			return;
		}

		wp_enqueue_style(
			'quicker-deactivation-feedback',
			\Quicker::assets_url() . 'css/deactivation-feedback.css',
			array(),
			\Quicker::get_version()
		);

		wp_enqueue_script(
			'quicker-deactivation-feedback',
			\Quicker::assets_url() . 'js/deactivation-feedback.js',
			array( 'jquery' ),
			\Quicker::get_version(),
			true
		);

		wp_localize_script(
			'quicker-deactivation-feedback',
			'quicker_deactivation',
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'nonce'    => wp_create_nonce( 'quicker_deactivation_feedback' ),
				'slug'     => dirname( $this->plugin_basename ),
			)
		);
	}

	/**
	 * Get feedback reasons.
	 *
	 * @return array
	 */
	private function get_reasons() {
		return array(
			'no_longer_needed'        => esc_html__( 'I no longer need the plugin', 'quicker' ),
			'found_better_plugin'     => esc_html__( 'I found a better plugin', 'quicker' ),
			'could_not_get_to_work'   => esc_html__( 'I couldn\'t get the plugin to work', 'quicker' ),
			'temporary_deactivation'  => esc_html__( 'It\'s a temporary deactivation', 'quicker' ),
			'plugin_broke_site'       => esc_html__( 'The plugin broke my site', 'quicker' ),
			'plugin_suddenly_stopped' => esc_html__( 'The plugin suddenly stopped working', 'quicker' ),
			'missing_feature'         => esc_html__( 'I need a specific feature that is missing', 'quicker' ),
			'other'                   => esc_html__( 'Other', 'quicker' ),
		);
	}

	/**
	 * Render the feedback modal HTML in the admin footer.
	 *
	 * @return void
	 */
	public function render_modal() {
		if ( ! $this->is_plugins_page() ) {
			return;
		}

		$reasons = $this->get_reasons();
		?>
		<div id="quicker-deactivation-modal" class="quicker-deactivation-modal" style="display:none;">
			<div class="quicker-deactivation-modal-overlay"></div>
			<div class="quicker-deactivation-modal-content">
				<div class="quicker-deactivation-modal-header">
					<h3><?php esc_html_e( 'Quick Feedback', 'quicker' ); ?></h3>
					<p><?php esc_html_e( 'If you have a moment, please let us know why you are deactivating Quicker:', 'quicker' ); ?></p>
				</div>
				<div class="quicker-deactivation-modal-body">
					<ul class="quicker-deactivation-reasons">
						<?php foreach ( $reasons as $key => $label ) : ?>
							<li>
								<label>
									<input type="radio" name="quicker_deactivation_reason" value="<?php echo esc_attr( $key ); ?>" />
									<span><?php echo esc_html( $label ); ?></span>
								</label>
								<?php if ( in_array( $key, array( 'found_better_plugin', 'missing_feature', 'other' ), true ) ) : ?>
									<input type="text"
										class="quicker-deactivation-detail"
										style="display:none;"
										placeholder="<?php
										if ( 'found_better_plugin' === $key ) {
											esc_attr_e( 'Please share the plugin name', 'quicker' );
										} elseif ( 'missing_feature' === $key ) {
											esc_attr_e( 'Please describe the feature', 'quicker' );
										} else {
											esc_attr_e( 'Please share your reason', 'quicker' );
										}
										?>"
									/>
								<?php endif; ?>
							</li>
						<?php endforeach; ?>
					</ul>
				</div>
				<div class="quicker-deactivation-modal-footer">
					<button class="button button-secondary quicker-deactivation-skip">
						<?php esc_html_e( 'Skip & Deactivate', 'quicker' ); ?>
					</button>
					<button class="button button-primary quicker-deactivation-submit" disabled>
						<?php esc_html_e( 'Submit & Deactivate', 'quicker' ); ?>
					</button>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Handle the AJAX feedback submission.
	 *
	 * @return void
	 */
	public function handle_feedback() {
		check_ajax_referer( 'quicker_deactivation_feedback', 'nonce' );

		$reason = isset( $_POST['reason'] ) ? sanitize_text_field( wp_unslash( $_POST['reason'] ) ) : '';
		$detail = isset( $_POST['detail'] ) ? sanitize_textarea_field( wp_unslash( $_POST['detail'] ) ) : '';

		if ( empty( $reason ) ) {
			wp_send_json_error( array( 'message' => 'No reason provided.' ) );
		}

		$body = array(
			'plugin_name'    => 'quicker',
			'reason'         => $reason,
			'feedback'       => $detail,
			'site_url'       => home_url(),
		);

		wp_remote_post(
			$this->api_url,
			array(
				'timeout'   => 15,
				'blocking'  => false,
				'sslverify' => false,
				'body'      => $body,
			)
		);

		wp_send_json_success();
	}
}
