<?php

namespace KaizenCoders\MagicLink;

class Ajax {
	/**
	 * Initialize the AJAX handlers.
	 */
	public function init() {
		add_action( 'wp_ajax_magic_link_manage_plugin', [ $this, 'handle_plugin_management' ] );
	}

	/**
	 * Handle plugin installation, activation, and deactivation via AJAX.
	 */
	public function handle_plugin_management() {
		check_ajax_referer( 'magic-link-plugin-management', 'nonce' );

		if ( ! current_user_can( 'activate_plugins' ) ) {
			wp_send_json_error( [ 'message' => 'Permission denied' ] );
		}

		$action = isset( $_POST['plugin_action'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_action'] ) ) : '';
		$plugin = isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '';
		$slug   = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';

		switch ( $action ) {
			case 'install':
				include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
				include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

				$api = plugins_api( 'plugin_information', [ 'slug' => $slug ] );
				if ( is_wp_error( $api ) ) {
					wp_send_json_error( [ 'message' => $api->get_error_message() ] );
				}

				$upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
				$result   = $upgrader->install( $api->download_link );

				if ( is_wp_error( $result ) ) {
					wp_send_json_error( [ 'message' => $result->get_error_message() ] );
				}
				break;

			case 'activate':
				$result = activate_plugin( $plugin );
				if ( is_wp_error( $result ) ) {
					wp_send_json_error( [ 'message' => $result->get_error_message() ] );
				}
				break;

			case 'deactivate':
				deactivate_plugins( [ $plugin ] );
				break;

			default:
				wp_send_json_error( [ 'message' => 'Invalid action' ] );
		}

		wp_send_json_success();
	}
}
