<?php
/**
 * Truebe deactivate survey: one optional question when the plugin is
 * deactivated from the Plugins page. Skipping or closing deactivates
 * immediately. The answer is sent to truebe.io from the browser, so
 * deactivation never waits on a remote request.
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( class_exists( 'Truebe_Deactivate_Survey' ) ) { return; }

class Truebe_Deactivate_Survey {

	const FEEDBACK_URL = 'https://truebe.io/api/feedback';

	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ) );
	}

	public static function enqueue( $hook ) {
		if ( 'plugins.php' !== $hook ) { return; }

		wp_enqueue_script(
			'truebe-deactivate-survey',
			plugins_url( '../assets/deactivate-survey.js', __FILE__ ),
			array(),
			(string) filemtime( plugin_dir_path( __FILE__ ) . '../assets/deactivate-survey.js' ),
			true
		);

		wp_localize_script( 'truebe-deactivate-survey', 'truebeDeactivate', array(
			'feedbackUrl' => self::FEEDBACK_URL,
			'site'        => home_url(),
			'version'     => defined( 'TRUEBE_VERSION' ) ? TRUEBE_VERSION : '',
			'title'       => __( 'One quick question', 'truebe-fact-based-ai-chatbot' ),
			'question'    => __( 'Why are you deactivating Truebe?', 'truebe-fact-based-ai-chatbot' ),
			'reasons'     => array(
				'not_working'   => __( "It didn't work as expected", 'truebe-fact-based-ai-chatbot' ),
				'missing'       => __( "It's missing a feature I need", 'truebe-fact-based-ai-chatbot' ),
				'better_option' => __( 'I found a better option', 'truebe-fact-based-ai-chatbot' ),
				'temporary'     => __( 'Temporary, I\'ll be back', 'truebe-fact-based-ai-chatbot' ),
			),
			'detailPlaceholder' => __( 'Anything else? (optional)', 'truebe-fact-based-ai-chatbot' ),
			'submitLabel'       => __( 'Send & deactivate', 'truebe-fact-based-ai-chatbot' ),
			'skipLabel'         => __( 'Skip & deactivate', 'truebe-fact-based-ai-chatbot' ),
		) );
	}
}

Truebe_Deactivate_Survey::init();