<?php
/**
 * Deactivation Feedback Modal Handler
 *
 * Enqueues assets, localizes script data, and renders the modal HTML
 * for the plugin deactivation feedback flow.
 *
 * All AJAX handling is in class-captwiki-deactivate-ajax.php.
 *
 * @package    captain-widgets-kit
 * @subpackage captain-widgets-kit/includes/admin
 * @since      1.0.0
 */

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

if ( ! class_exists( 'Captwiki_Deactivate_Feedback' ) ) {

	/**
	 * Captwiki_Deactivate_Feedback
	 *
	 * Renders the deactivation feedback modal and enqueues its assets.
	 * The JS calls WordPress AJAX actions (not the remote API directly).
	 *
	 * @since 1.0.0
	 */
	class Captwiki_Deactivate_Feedback {

		/**
		 * Singleton instance.
		 *
		 * @since 1.0.0
		 * @var Captwiki_Deactivate_Feedback
		 */
		private static $instance;

		/**
		 * Initiator — returns the single instance.
		 *
		 * @since  1.0.0
		 *
		 * @return Captwiki_Deactivate_Feedback
		 */
		public static function get_instance() {
			if ( ! isset( self::$instance ) ) {
				self::$instance = new self();
			}
			return self::$instance;
		}

		/**
		 * Constructor — register WordPress hooks.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
			add_action( 'admin_footer', array( $this, 'render_modal' ) );
		}

		/**
		 * Enqueue CSS and JS on the plugins list screen only.
		 *
		 * @since  1.0.0
		 *
		 * @param  string $hook  Current admin page hook suffix.
		 *
		 * @return void
		 */
		public function enqueue_assets( $hook ) {
			if ( 'plugins.php' !== $hook ) {
				return;
			}

			wp_enqueue_style(
				'cwk-deactivate-feedback',
				CAPTWIKI_URL . 'assets/css/deactivate-feedback.css',
				array(),
				CAPTWIKI_VERSION
			);

			wp_enqueue_script(
				'cwk-deactivate-feedback',
				CAPTWIKI_URL . 'assets/js/deactivate-feedback.js',
				array( 'jquery' ),
				CAPTWIKI_VERSION,
				true
			);

			wp_localize_script(
				'cwk-deactivate-feedback',
				'cwkDeactivate',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'cwk_deactivate_nonce' ),
				)
			);
		}

		/**
		 * Output the deactivation feedback modal HTML in the admin footer.
		 *
		 * Only rendered on the plugins list screen (plugins.php).
		 *
		 * @since  1.0.0
		 *
		 * @return void
		 */
		public function render_modal() {
			$screen = get_current_screen();

			if ( ! $screen || 'plugins' !== $screen->id ) {
				return;
			}

			// Load plugin logo SVG inline so WordPress admin CSS cannot override it.
			$svg_path = CAPTWIKI_PATH . 'assets/svg/cwk-logo-sw.svg';
			$icon_svg = '';
			if ( file_exists( $svg_path ) ) {
				$raw      = file_get_contents( $svg_path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
				$icon_svg = preg_replace( '/width="[^"]+"/', 'width="22"', $raw, 1 );
				$icon_svg = preg_replace( '/height="[^"]+"/', 'height="22"', $icon_svg, 1 );
			}
			?>
			<div id="cwk-deactivate-overlay" style="display:none;">
				<div id="cwk-deactivate-modal" role="dialog" aria-modal="true" aria-labelledby="cwk-modal-title">

					<div class="cwk-modal-header">
						<span class="cwk-modal-logo" aria-hidden="true">
							<?php echo $icon_svg; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- sanitized SVG from plugin's own file ?>
						</span>
						<span class="cwk-modal-title" id="cwk-modal-title">
							<?php echo esc_html__( 'Quick Feedback', 'captain-widgets-kit' ); ?>
						</span>
						<button class="cwk-modal-close" id="cwk-modal-close" aria-label="<?php echo esc_attr__( 'Close', 'captain-widgets-kit' ); ?>">&times;</button>
					</div>

					<p class="cwk-modal-question">
						<?php echo esc_html__( 'If you have a moment, please share why you are deactivating', 'captain-widgets-kit' ); ?>
						<strong><?php echo esc_html__( 'Captain Widgets Kit', 'captain-widgets-kit' ); ?></strong>:
					</p>

					<div class="cwk-thumb-row">
						<button class="cwk-thumb-btn cwk-thumb-active" id="cwk-thumb-like" data-val="like">
							<span class="cwk-thumb-icon" aria-hidden="true">&#128077;</span>
							<span><?php echo esc_html__( 'Like it', 'captain-widgets-kit' ); ?></span>
						</button>
						<button class="cwk-thumb-btn" id="cwk-thumb-notlike" data-val="notlike">
							<span class="cwk-thumb-icon" aria-hidden="true">&#128078;</span>
							<span><?php echo esc_html__( 'Not Like it', 'captain-widgets-kit' ); ?></span>
						</button>
					</div>

					<!-- Like it: optional message textarea (visible by default) -->
					<div id="cwk-like-section">
						<textarea
							id="cwk-like-message"
							rows="3"
							placeholder="<?php echo esc_attr__( 'Want to share anything else? (optional)', 'captain-widgets-kit' ); ?>"
						></textarea>
					</div>

					<!-- Not Like it: reasons list + optional detail textarea -->
					<div id="cwk-reason-section" style="display:none;">
						<div class="cwk-reasons" role="radiogroup" aria-label="<?php echo esc_attr__( 'Deactivation reason', 'captain-widgets-kit' ); ?>">

							<label class="cwk-reason-label">
								<input type="radio" name="cwk_reason" value="missing_features" />
								<span class="cwk-radio-circle"></span>
								<?php echo esc_html__( 'Missing features I need', 'captain-widgets-kit' ); ?>
							</label>

							<label class="cwk-reason-label">
								<input type="radio" name="cwk_reason" value="compatibility_issue" />
								<span class="cwk-radio-circle"></span>
								<?php echo esc_html__( 'Compatibility issue with my theme or plugin', 'captain-widgets-kit' ); ?>
							</label>

							<label class="cwk-reason-label">
								<input type="radio" name="cwk_reason" value="performance_issue" />
								<span class="cwk-radio-circle"></span>
								<?php echo esc_html__( 'It affected my site performance', 'captain-widgets-kit' ); ?>
							</label>

							<label class="cwk-reason-label">
								<input type="radio" name="cwk_reason" value="temporary" />
								<span class="cwk-radio-circle"></span>
								<?php echo esc_html__( 'Temporarily deactivating for troubleshooting', 'captain-widgets-kit' ); ?>
							</label>

							<label class="cwk-reason-label">
								<input type="radio" name="cwk_reason" value="other" />
								<span class="cwk-radio-circle"></span>
								<?php echo esc_html__( 'Other', 'captain-widgets-kit' ); ?>
							</label>

						</div>

						<div id="cwk-message-wrap" style="display:none;">
							<textarea
								id="cwk-message"
								rows="3"
								placeholder="<?php echo esc_attr__( 'Please tell us more...', 'captain-widgets-kit' ); ?>"
							></textarea>
						</div>
					</div>

					<div class="cwk-modal-footer">
						<button id="cwk-submit-btn" class="cwk-btn-primary">
							<?php echo esc_html__( 'Submit & Deactivate', 'captain-widgets-kit' ); ?>
						</button>
						<button id="cwk-skip-btn" class="cwk-btn-ghost">
							<?php echo esc_html__( 'Skip & Deactivate', 'captain-widgets-kit' ); ?>
						</button>
					</div>

				</div>
			</div>
			<?php
		}
	}

	Captwiki_Deactivate_Feedback::get_instance();
}
