<?php
/**
 *  This file contains the whole feedback form to be displayed at the time of deactivation of the plugin.
 *
 * @package object-data-sync-for-salesforce
 */

namespace MoSfSyncSalesforce;

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

use MoSfSyncSalesforce\Helper\Plugin_Constants;
use MoSfSyncSalesforce\Services\Utils;

/**
 * Predefined checkbox reasons for the deactivation form (key => translatable label).
 *
 * @return array<string,string>
 */
function mo_sf_sync_get_feedback_form_reason_options() {
	return array(
		'setup_too_difficult'   => __( 'Setup or configuration was too difficult or took too long', 'object-data-sync-for-salesforce' ),
		'missing_features'      => __( 'The plugin is missing features I need', 'object-data-sync-for-salesforce' ),
		'costly_to_upgrade'     => __( 'The plugin is too costly to upgrade to the Premium version', 'object-data-sync-for-salesforce' ),
		'found_alternative'     => __( 'I am switching to a different tool or integration', 'object-data-sync-for-salesforce' ),
		'salesforce_connection' => __( 'I could not connect to Salesforce or the connection kept failing', 'object-data-sync-for-salesforce' ),
		'other'                 => __( 'Something else (add a short note below)', 'object-data-sync-for-salesforce' ),
	);
}

/**
 * Allowed POST values for mo_sf_sync_feedback_reasons[] (for server-side validation).
 *
 * @return array<int,string>
 */
function mo_sf_sync_get_feedback_form_allowed_reason_keys() {
	return array_keys( mo_sf_sync_get_feedback_form_reason_options() );
}

/**
 * Displays the feedback form at the time of plugin deactivation.
 *
 * @return void
 */
function mo_sf_sync_display_feedback_form() {
	if ( isset( $_SERVER['PHP_SELF'] ) && 'plugins.php' !== basename( sanitize_text_field( wp_unslash( $_SERVER['PHP_SELF'] ) ) ) ) {
		return;
	}
	wp_enqueue_style( 'wp-pointer' );
	wp_enqueue_script( 'wp-pointer' );
	wp_enqueue_script( 'utils' );
	wp_enqueue_style( 'mo_sf_sync_admin_plugins_page_style', Utils::mo_sf_sync_get_settings_css_path_url(), array(), Plugin_Constants::VERSION );
	wp_enqueue_script( 'mo-sf-feedback-form-js', Utils::mo_sf_sync_get_feedback_form_js_path_url(), array( 'jquery' ), Plugin_Constants::VERSION, true );

	$reason_options = mo_sf_sync_get_feedback_form_reason_options();

	wp_localize_script(
		'mo-sf-feedback-form-js',
		'moSfSyncDeactivationFeedback',
		array(
			'pluginBasename' => plugin_basename( MOSF_FILE ),
		)
	);

	$user  = wp_get_current_user();
	$email = $user->user_email;
	?>
	<div id="mo_sf_sync_feedback_modal" class="mo_sf_sync_modal mo_sf_sync_feedback_modal_wrap" role="dialog" aria-modal="true" aria-labelledby="mo_sf_sync_feedback_title" aria-hidden="true">
		<div class="mo_sf_sync_modal-content">
			<div class="mo_sf_sync_feedback_header">
				<h3 id="mo_sf_sync_feedback_title" class="mo_sf_sync_feedback_title">
					<?php esc_html_e( 'Sorry to see you go! Quick feedback', 'object-data-sync-for-salesforce' ); ?>
				</h3>
				<button type="button" class="mo_sf_sync_close mo_sf_sync_feedback_close" aria-label="<?php esc_attr_e( 'Close this dialog', 'object-data-sync-for-salesforce' ); ?>">
					&times;
				</button>
			</div>
			<form name="f" method="post" action="" id="mo_sf_sync_feedback">
				<?php wp_nonce_field( 'mo_sf_sync_feedback' ); ?>
				<input type="hidden" name="option" value="mo_sf_sync_feedback" />
				<div class="mo_sf_sync_feedback_body">
					<p class="mo_sf_sync_feedback_question">
						<?php esc_html_e( 'What best describes why you are deactivating? You can select more than one option.', 'object-data-sync-for-salesforce' ); ?>
					</p>
					<?php if ( ! empty( $reason_options ) ) : ?>
					<ul class="mo_sf_sync_feedback_reasons">
						<?php foreach ( $reason_options as $reason_key => $reason_label ) : ?>
						<li>
							<input
								type="checkbox"
								name="mo_sf_sync_feedback_reasons[]"
								id="<?php echo esc_attr( 'mo_sf_sync_reason_' . $reason_key ); ?>"
								value="<?php echo esc_attr( $reason_key ); ?>"
							/>
							<label for="<?php echo esc_attr( 'mo_sf_sync_reason_' . $reason_key ); ?>">
								<?php echo esc_html( $reason_label ); ?>
							</label>
						</li>
						<?php endforeach; ?>
					</ul>
					<?php endif; ?>
					<label class="screen-reader-text" for="query_feedback">
						<?php esc_html_e( 'Optional details about your experience', 'object-data-sync-for-salesforce' ); ?>
					</label>
					<textarea
						id="query_feedback"
						class="mo_sf_sync_feedback_textarea"
						name="query_feedback"
						rows="4"
						placeholder="<?php esc_attr_e( 'Optional: tell us more so we can improve the plugin.', 'object-data-sync-for-salesforce' ); ?>"
					></textarea>
					<div class="mo_sf_sync_feedback_email_row">
						<div class="mo_sf_sync_feedback_email_wrap">
							<input
								type="email"
								id="mo_sf_sync_query_mail"
								class="mo_sf_sync_query_mail"
								name="query_mail"
								placeholder="<?php esc_attr_e( 'Your email address', 'object-data-sync-for-salesforce' ); ?>"
								required
								readonly
								value="<?php echo esc_attr( $email ); ?>"
							/>
							<button type="button" class="mo_sf_sync_feedback_edit_mail button-link" aria-label="<?php esc_attr_e( 'Change the email address we should use', 'object-data-sync-for-salesforce' ); ?>">
								<?php
								Utils::mo_sf_sync_render_image(
									'61456.png',
									array(
										'alt'   => '',
										'class' => 'mo_sf_sync_feedback_edit_mail',
									)
								);
								?>
							</button>
						</div>
					</div>
					<div class="mo_sf_sync_feedback_checkbox_block">
						<label>
							<input type="checkbox" name="get_reply" value="yes" checked />
							<?php esc_html_e( 'Allow MiniOrange Team to connect via email for prompt issue resolution by your experience.', 'object-data-sync-for-salesforce' ); ?>
						</label>
					</div>
					<div class="mo_sf_sync_feedback_checkbox_block">
						<label>
							<input type="checkbox" name="mo_sf_sync_anonymous_usage" value="1" />
							<?php esc_html_e( 'Help us improve the plugin by including anonymous usage data with this message. This does not include any personal information.', 'object-data-sync-for-salesforce' ); ?>
						</label>
					</div>
				</div>
				<div class="mo_sf_sync_feedback_footer mo_sf_sync_modal-footer">
					<button type="submit" name="mo_sf_sync_feedback_action" class="button button-primary button-large" value="skip">
						<?php esc_html_e( 'Skip feedback', 'object-data-sync-for-salesforce' ); ?>
					</button>
					<button type="submit" name="mo_sf_sync_feedback_action" class="button button-primary button-large" value="send">
						<?php esc_html_e( 'Submit and deactivate', 'object-data-sync-for-salesforce' ); ?>
					</button>
				</div>
			</form>
		</div>
	</div>
	<?php
}
