<?php

/**
 * Fired during plugin deactivation.
 *
 * This class defines all code necessary to run during the plugin's deactivation.
 *
 * @since      1.0.0
 * @package    Verfacto_Plugin
 * @subpackage Verfacto_Plugin/includes
 * @author     Verfacto <support@verfacto.com>
 */

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/trait-verfacto-plugin-helper.php';
class Verfacto_Plugin_Deactivator {

	use Verfacto_Plugin_Helper;

	/**
	 * Runs on plugin deactivation hook.
	 */
	public function deactivate() {
		if ( (bool) $this->get_setting_by_name( 'verfacto_plugin_active' ) ) {
			global $wpdb;
			$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_api_keys WHERE description LIKE '%Verfacto - API%'" );
		}

		$verfacto_options                             = get_option( 'verfacto_options' );
		$verfacto_options['verfacto_plugin_active']   = false;
		$verfacto_options['verfacto_account_id']      = null;
		$verfacto_options['verfacto_user_email']      = null;
		$verfacto_options['verfacto_login_token']     = null;
		$verfacto_options['verfacto_tracker_id']      = null;
		$verfacto_options['verfacto_consumer_key']    = null;
		$verfacto_options['verfacto_consumer_secret'] = null;
		update_option( 'verfacto_options', $verfacto_options );

	}

	/**
	 * Deactivate Verfacto plugin sending request to Verfacto API to schedule account deletion
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function deactivate_verfacto() {
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			wp_send_json_error( 'Insufficient permissions', 403 );
		}
		check_ajax_referer( 'verfacto_nonce', 'vf_nonce' );

		$email    = isset( $_POST['user_email'] ) ? sanitize_email( $_POST['user_email'] ) : '';
		$password = isset( $_POST['user_password'] ) ? sanitize_text_field( $_POST['user_password'] ) : '';

		if ( ! is_email( $email ) ) {
			wp_send_json_error( 'Looks like your email is invalid.', 403 );
		}

		if ( strlen( $password ) < 8 || strlen( $password ) > 128 ) {
			wp_send_json_error( 'Looks like your password is incorrect length. Password must be between 8 and 128 character', 403 );
		}

		$auth = $this->authorize_backoffice_access( $email, $password );

		if ( 200 !== (int) $auth['response_code'] ) {
			$body = isset( $auth['response_body'] ) ? $auth['response_body'] : [];
			$error_msg = ! empty( $body['message'] ) ? $body['message'] : 'Something went wrong. Please try again later.';

			wp_send_json_error( $error_msg, (int) $auth['response_code'] );
		}

		$payload = array(
			'to_delete_at'      => gmdate( 'Y-m-d H:i:s', strtotime( '+ 14 DAY' ) ),
			'send_notification' => true,
		);

		$del_resp = $this->call_api_endpoint( 'PUT', $payload, '/accounts/' . $this->get_setting_by_name( 'verfacto_account_id' ) . '/schedule-account-deletion', $auth['response_body']['auth_token'] );

		if ( 200 !== (int) $del_resp['response_code'] ) {
			$body      = $del_resp['response_body'] ?? [];
			$error_msg = $body['message'] ?? 'Could not deactivate plugin.';
			wp_send_json_error( $error_msg, (int) $del_resp['response_code'] );
		}

		// Delete all Verfacto options and API keys
		$this->deactivate();

		wp_send_json_success();
	}


	public function print_deactivate_modal() {
		ob_start();
		include_once plugin_dir_path( dirname( __FILE__ ) ) . '/admin/pages/modals/deactivation-modal.php';
		echo ob_get_clean();
	}
}
