<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
class Sifency_Addons_Deactivation_Feedback {

	private $plugin_slug = 'sifency-addons';
	private $plugin_file = 'sifency-addons.php';

	public function __construct() {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
		add_action( 'wp_ajax_sifency_submit_deactivation_feedback', array( $this, 'handle_feedback_ajax' ) );
		add_action( 'wp_ajax_sifency_set_uninstall_flag', array( $this, 'handle_uninstall_flag_ajax' ) );

		register_activation_hook( __FILE__, array( $this, 'clear_uninstall_flag' ) );
	}

	public function clear_uninstall_flag() {
		delete_option( 'sifency_uninstall_flag' );
	}

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

		// The base path to your plugin file.
		$plugin_basename = $this->plugin_slug . '/' . $this->plugin_file;

		wp_enqueue_style( 'sifency-deactivation-feedback', SIFENCY_ADDONS_URL . 'assets/css/admin/admin-deactivation-feedback.css', array(), SIFENCY_ADDONS_VERSION );
		wp_enqueue_script( 'sifency-deactivation-feedback', SIFENCY_ADDONS_URL . 'assets/js/admin/admin-deactivation-feedback.js', array( 'jquery' ), SIFENCY_ADDONS_VERSION, true );

		// Pass data to JavaScript
		wp_localize_script(
			'sifency-deactivation-feedback',
			'sifencyDeactivation',
			array(
				'ajax_url'        => admin_url( 'admin-ajax.php' ),
				'nonce'           => wp_create_nonce( 'sifency_deactivation_nonce' ),
				'plugin_basename' => $plugin_basename,
				'i18n'            => array(
					'submit_deactivate' => esc_html__( 'Submit and Deactivate', 'sifency-addons' ),
					'submit_delete'     => esc_html__( 'Submit and Delete Data', 'sifency-addons' ),
					'skip_deactivate'   => esc_html__( 'Skip and Deactivate', 'sifency-addons' ),
					'skip_delete'       => esc_html__( 'Just Delete Data', 'sifency-addons' ),
				),
			)
		);

		// Add the modal HTML to the footer of the plugins page.
		add_action( 'admin_footer-plugins.php', array( $this, 'render_modal_html' ) );
	}

	public function render_modal_html() {
		?>
		<div class="sifency-feedback-modal-overlay" style="display: none;"></div>
		<div class="sifency-feedback-modal" style="display: none;">
			<div class="sifency-feedback-modal-header">
				<h3><?php esc_html_e( 'Quick Feedback', 'sifency-addons' ); ?></h3>
				<p><?php esc_html_e( 'We\'re sorry to see you go. Could you please take 10 seconds to tell us why you are deactivating the plugin? Your feedback is invaluable.', 'sifency-addons' ); ?></p>
			</div>
			<div class="sifency-feedback-modal-body">

				<fieldset class="sifency-data-choice-section">
					<legend><?php esc_html_e( 'Data Management', 'sifency-addons' ); ?></legend>
					<p class="description"><?php esc_html_e( 'What should we do with your data?', 'sifency-addons' ); ?></p>
					<ul>
						<li><label><input type="radio" name="sifency_data_choice" value="keep" checked> <?php esc_html_e( 'Keep all data', 'sifency-addons' ); ?> <small>(Default & Recommended)</small></label></li>
						<li>
							<label class="sifency-danger-color"><input type="radio" name="sifency_data_choice" value="delete"> <?php esc_html_e( 'Permanently delete all data (Warning: This will permanently delete all Sifency posts, settings, and content. This cannot be undone.)', 'sifency-addons' ); ?></label>
							<p class="sifency-data-choice-warning"><?php esc_html_e( 'Warning: This will permanently delete all Sifency posts, settings, and content. This cannot be undone.', 'sifency-addons' ); ?></p>
						</li>
					</ul>
				</fieldset>


				<ul class="sifency-feedback-reasons">
					<li><label><input type="radio" name="sifency_feedback_reason" value="temporary_deactivation"> <?php esc_html_e( 'It\'s a temporary deactivation.', 'sifency-addons' ); ?></label></li>
					<li><label><input type="radio" name="sifency_feedback_reason" value="found_better_plugin"> <?php esc_html_e( 'I found a better plugin.', 'sifency-addons' ); ?></label></li>
					<li><label><input type="radio" name="sifency_feedback_reason" value="missing_feature"> <?php esc_html_e( 'The plugin is missing a feature.', 'sifency-addons' ); ?></label></li>
					<li><label><input type="radio" name="sifency_feedback_reason" value="plugin_broke_site"> <?php esc_html_e( 'The plugin broke my site.', 'sifency-addons' ); ?></label></li>
					<li><label><input type="radio" name="sifency_feedback_reason" value="buggy"> <?php esc_html_e( 'The plugin is buggy.', 'sifency-addons' ); ?></label></li>
					<li><label><input type="radio" name="sifency_feedback_reason" value="other"> <?php esc_html_e( 'Other', 'sifency-addons' ); ?></label></li>
				</ul>
				<textarea class="sifency-feedback-other sifency-admin-btn" placeholder="<?php esc_attr_e( 'Please specify...', 'sifency-addons' ); ?>" style="display: none;"></textarea>
			</div>
			<div class="sifency-feedback-modal-footer">
				<div class="sifency-btn-group sifency-modal-footer">
					<button class="button button-primary sifency-feedback-submit sifency-admin-btn sifency-admin-accent-btn"></button>
					<button class="button button-secondary sifency-feedback-skip sifency-admin-btn"></button>
				</div>
				<button class="button-link sifency-feedback-cancel sifency-admin-btn"><?php esc_html_e( 'Cancel', 'sifency-addons' ); ?></button>
			</div>
		</div>
		<?php
	}

	/**
	 * Handles the AJAX request for sending deactivation feedback.
	 * This method ONLY handles the feedback email.
	 */
	public function handle_feedback_ajax() {
		// Verify nonce properly
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'sifency_deactivation_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'Security verification failed.' ), 403 );
		}

		if ( ! current_user_can( 'activate_plugins' ) ) {
			wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 );
		}

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

		if ( empty( $reason ) && empty( $other_info ) ) {
			wp_send_json_success( array( 'message' => 'No feedback provided.' ) );
			return;
		}

		$recipient_email = 'hello.sifency@gmail.com';
		$subject         = 'Plugin Deactivation Feedback: Sifency Addons';

		$message  = "A user has deactivated the Sifency Addons plugin.\n\n";
		$message .= 'Site URL: ' . home_url() . "\n";
		$message .= 'WordPress Version: ' . get_bloginfo( 'version' ) . "\n\n";

		if ( ! empty( $reason ) ) {
			$message .= 'Reason: ' . $reason . "\n";
		}
		if ( ! empty( $other_info ) ) {
			$message .= 'Details: ' . $other_info . "\n";
		}

		wp_mail( $recipient_email, $subject, $message );

		wp_send_json_success( array( 'message' => 'Feedback sent.' ) );
	}


	/**
	 * Handles the data deletion choice.
	 */
	public function handle_uninstall_flag_ajax() {
		// Verify nonce properly
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'sifency_deactivation_nonce' ) ) {
			wp_send_json_error( array( 'message' => 'Security verification failed.' ), 403 );
		}

		if ( ! current_user_can( 'delete_plugins' ) ) {
			wp_send_json_error( array( 'message' => 'Permission denied.' ), 403 );
		}

		$choice = isset( $_POST['data_choice'] ) ? sanitize_text_field( $_POST['data_choice'] ) : 'keep';

		if ( 'delete' === $choice ) {
			update_option( 'sifency_uninstall_flag', 'delete_all' );
			wp_send_json_success( array( 'message' => 'Flag set to delete.' ) );
		} else {
			delete_option( 'sifency_uninstall_flag' );
			wp_send_json_success( array( 'message' => 'Flag cleared (Keep data).' ) );
		}
	}
}
new Sifency_Addons_Deactivation_Feedback();
