<?php
/**
 * Coupons Process.
 *
 * @package EverestForms\Pro\Addons\Coupons\Process
 * @since   3.0.5
 */

namespace EverestForms\Pro\Addons\Coupons\Process;

use EverestForms\Pro\Addons\Coupons\CouponsListTable\CouponsListTable;
use EverestForms\Pro\Addons\Coupons\CouponForm;
use EverestForms\Pro\Addons\Coupons\BulkCouponForm;
use EVF_Bulk_Coupon_Generator;

defined( 'ABSPATH' ) || exit;

/**
 * Process Class.
 *
 * @since 3.0.5
 */
class Process {

	private static $background_process;

	/**
	 * Constructor.
	 *
	 * @since 3.0.5
	 *
	 * @return void
	 */
	public function __construct() {
		add_action( 'admin_menu', array( $this, 'coupons_menu' ), 35 );
		add_action( 'admin_init', array( $this, 'register_post_types' ), 5 );
		add_action( 'admin_init', array( $this, 'insert_coupon' ) );
		if ( isset( $_POST['evf-bulk-coupon'] ) && $_POST['evf-bulk-coupon'] === 'Save' ) {
			add_action( 'admin_init', array( $this, 'generate_bulk_coupon_code' ) );
		}
		add_action( 'admin_init', array( $this, 'update_coupon' ) );
		add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 11, 3 );
		add_filter( 'screen_options_show_screen', array( $this, 'remove_screen_options' ) );
		add_filter( 'everest_forms_screen_ids', array( $this, 'evf_coupons_add_screen_id' ) );
		add_action( 'wp_ajax_everest_forms_coupons_apply', array( $this, 'check_coupon' ) );
		add_action( 'wp_ajax_nopriv_everest_forms_coupons_apply', array( $this, 'check_coupon' ) );

		add_filter( 'everest_forms_coupon_discount', array( $this, 'coupon_discount' ), 10, 4 );
		add_filter( 'everest_forms_coupon_discount_map_field', array( $this, 'discount_map_field' ), 10, 2 );
		include_once plugin_dir_path( EFP_PLUGIN_FILE ) . 'src/Addons/Coupons/class-evf-bulk-coupon-generator.php';

		self::$background_process = new EVF_Bulk_Coupon_Generator();
	}


	/**
	 * Discount Map Field.
	 *
	 * @param mixed $field Field.
	 * @param mixed $form_data Form Data.
	 * @return mixed Map field.
	 */
	public function discount_map_field( $field, $form_data ) {
		foreach ( $form_data['form_fields'] as $field ) {
			if ( 'payment-coupon' === $field['type'] ) {
				return $field['map_field'];
			}
		}
	}

	/**
	 * Coupon Discount.
	 *
	 * @param mixed $discount Discount.
	 * @param mixed $total Total.
	 * @param mixed $fields Fields.
	 * @param mixed $form_data Form Data.
	 * @return mixed $discount Discount.
	 */
	public function coupon_discount( $discount, $total, $fields, $form_data ) {
		foreach ( $fields as $field ) {
			if ( 'payment-coupon' === $field['type'] ) {
				$coupon_field          = $form_data['form_fields'][ $field['id'] ];
				$coupon_field['value'] = $field['value'];
				break;
			}
		}

		if ( ! empty( $coupon_field ) ) {
			$coupon_code = $coupon_field['value'];
			if ( empty( $coupon_code ) ) {
				return $discount;
			}
			$all_coupons = self::get_coupons();
			foreach ( $all_coupons as $coupon ) {
				$date = time();
				if ( $date >= strtotime( $coupon['start_date'] ) && $date <= strtotime( $coupon['end_date'] ) && '1' === $coupon['status'] && in_array( (int) $form_data['id'], $coupon['applicable_forms'], true ) && $coupon_code === $coupon['code'] ) {
					$map_field = ! empty( 'map_field' ) && ! empty( $fields[ $coupon_field['map_field'] ] ) ? $fields[ $coupon_field['map_field'] ] : '';
					if ( 'fixed' === $coupon['type'] ) {
						return preg_replace( '/^.*?\;/', '', $coupon['amount'] );
					}
					$coupon['amount'] = str_replace( '%', '', $coupon['amount'] );
					if ( ! empty( $map_field ) ) {
						return $map_field['amount_raw'] * $coupon['amount'] / 100;
					} else {
						return $total * $coupon['amount'] / 100;
					}
				}
			}
			return $discount;
		}
	}

	/**
	 * Validate Coupons.
	 *
	 * @return void
	 */
	public function check_coupon() {
		if ( ! isset( $_POST['action'] ) || ! isset( $_POST['nonce'] ) || 'everest_forms_coupons_apply' !== sanitize_title( wp_unslash( $_POST['action'] ) ) || wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ), 'everest_forms_coupons_nonce' ) ) ) {
			return;
		}

		$coupon_code = isset( $_POST['coupon_code'] ) ? sanitize_text_field( wp_unslash( $_POST['coupon_code'] ) ) : '';
		$coupon_id   = isset( $_POST['coupon_id'] ) ? sanitize_text_field( wp_unslash( $_POST['coupon_id'] ) ) : '';

		if ( empty( $coupon_code ) || empty( $coupon_id ) ) {
			wp_send_json_error( array( 'message' => __( 'Coupon code is missing', 'everest-forms-pro' ) ) );
		}

		$form_id = isset( $_POST['form_id'] ) ? sanitize_title( wp_unslash( $_POST['form_id'] ) ) : 0;

		if ( empty( $form_id ) ) {
			wp_send_json_error( array( 'message' => __( 'Form ID is missing', 'everest-forms-pro' ) ) );
		}

		$data = self::validate_coupon( $form_id, $coupon_id, $coupon_code );
		if ( ! empty( $data['status'] ) ) {
			wp_send_json_success( $data );
		}
		wp_send_json_error( $data );
	}

	/**
	 * Validate Coupon.
	 *
	 * @param mixed $form_id Form ID.
	 * @param mixed $coupon_id Coupon ID.
	 * @param mixed $coupon_code Coupon Code.
	 * @return mixed $data Coupon Data.
	 */
	public static function validate_coupon( $form_id, $coupon_id, $coupon_code ) {
		$form_data = evf()->form->get( $form_id, array( 'content_only' => true ) );

		if ( empty( $form_data['form_fields'][ $coupon_id ] ) ) {
			wp_send_json_error( array( 'message' => __( 'Coupon Field is removed or missing', 'everest-forms-pro' ) ) );
		}

		$coupon_field = $form_data['form_fields'][ $coupon_id ];

		$coupons = ! empty( $coupon_field['coupons'] ) ? explode( ',', $coupon_field['coupons'] ) : array();

		$all_coupons = self::get_coupons();

		foreach ( $all_coupons as $coupon ) {
			$date = time();
			if ( $date >= strtotime( $coupon['start_date'] ) && $date <= strtotime( $coupon['end_date'] ) && '1' === $coupon['status'] && in_array( (int) $form_id, $coupon['applicable_forms'], true ) && $coupon_code === $coupon['code'] ) {
				$map_field = ! empty( 'map_field' ) && ! empty( $form_data['form_fields'][ $coupon_field['map_field'] ] ) ? $form_data['form_fields'][ $coupon_field['map_field'] ] : '';
				if ( ! empty( $map_field ) ) {
					/* translators: 1: Label. 2: Discount */
					$message = sprintf( __( 'Discount on %1$s : %2$s', 'everest-forms-pro' ), $map_field['label'], $coupon['amount'] );
				} else {
					$total_field_label = __( 'Total', 'everest-forms-pro' );
					foreach ( $form_data['form_fields'] as $field ) {
						if ( 'payent-total' === $field['type'] ) {
							$total_field_label = $field['label'];
							break;
						}
					}
					/* translators: 1: Label. 2: Discount */
					$message = sprintf( __( 'Discount on %1$s : %2$s', 'everest-forms-pro' ), $total_field_label, $coupon['amount'] );
				}
				$data = array(
					'status'        => true,
					'message'       => __( 'Coupon validated successfully', 'everest-forms-pro' ),
					'discount_type' => $coupon['type'],
					'amount'        => $coupon['amount'],
					'map_field'     => $coupon_field['map_field'],
					'message'       => $message,
				);
				return $data;
			}
		}
		return array(
			'status'  => false,
			'message' => ! empty( $coupon_field['invalid_message'] ) ? esc_html( $coupon_field['invalid_message'] ) : esc_html__( 'Coupon code is invalid or expired', 'everest-forms-pro' ),
		);
	}

	/**
	 * Add menu item.
	 */
	public function coupons_menu() {
		$coupon_page = add_submenu_page( 'everest-forms', esc_html__( 'Coupons', 'everest-forms-pro' ), esc_html__( 'Coupons', 'everest-forms-pro' ), 'manage_everest_forms', 'evf-coupons', array( $this, 'coupons_page' ) );
		add_action( 'load-' . $coupon_page, array( $this, 'coupon_page_init' ) );
	}

	/**
	 * Loads template page.
	 */
	public function coupon_page_init() {
		global $CouponsListTable;
		$CouponsListTable = new CouponsListTable();

		// Add screen option.
		add_screen_option(
			'per_page',
			array(
				'default' => 20,
				'option'  => 'evf_coupons_per_page',
			)
		);

		do_action( 'everest_forms_coupons_page_init' );
	}

	/**
	 * Init the coupons page.
	 */
	public function coupons_page() {
		if ( isset( $_REQUEST['create-coupon'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			CouponForm::output_form_html();
		} elseif ( ( isset( $_REQUEST['action'] ) && 'edit' === $_REQUEST['action'] ) && isset( $_REQUEST['coupon'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			$coupon_id = absint( wp_unslash( $_REQUEST['coupon'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
			$post      = get_post( $coupon_id );
			$coupon    = ! empty( $post->post_content ) ? evf_decode( $post->post_content ) : '';
			CouponForm::output_form_html( 'edit', $coupon );
		} elseif ( isset( $_REQUEST['create-bulk-coupon'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			BulkCouponForm::output_bulk_coupon_form_html();
		} else {
			self::table_list_output();
		}
	}

	/**
	 * Table list output.
	 */
	public function table_list_output() {
		global $CouponsListTable;
		$CouponsListTable->process_bulk_action();
		$CouponsListTable->prepare_items();
		?>
			<div class="wrap evf-coupon__wrapper">
		<?php include_once plugin_dir_path( EFP_PLUGIN_FILE ) . 'src/Addons/Coupons/views/header.php'; ?>

		<?php settings_errors(); ?>
				<form id="form-list" method="post">
				<input type="hidden" name="page" value="evf-coupons" />
				<div class="everest-forms-coupons-list-table__wrapper">
					<div id="everest-forms-coupon-list-table__nav">
						<h3 id="everest-forms-coupons-list-table__heading"><?php esc_html_e( 'Coupons', 'everest-forms-pro' ); ?></h3>
						<button id="everest-forms-coupons__create-btn" class="everest-forms-btn everest-forms-btn-primary" style="padding: 10px 16px; line-height: 22px;"><span id="everest-forms-coupons-create-plus">+</span><?php esc_html_e( 'Create New Coupon', 'everest-forms-form' ); ?></button>
					</div>
		<?php
				$CouponsListTable->views();
				$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none">
					<path d="M8.25 14.25C11.5637 14.25 14.25 11.5637 14.25 8.25C14.25 4.93629 11.5637 2.25 8.25 2.25C4.93629 2.25 2.25 4.93629 2.25 8.25C2.25 11.5637 4.93629 14.25 8.25 14.25Z" stroke="#6B6B6B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
					<path d="M15.7508 15.7498L12.4883 12.4873" stroke="#6B6B6B" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
					</svg>';
				$CouponsListTable->search_box( $svg, 'search_input' );
				printf( '<hr/>' );
				$CouponsListTable->display();
				printf( '<div id="everest-forms-coupons__pagination-bottom">' );
					$CouponsListTable->pagination( 'bottom' );
				printf( '</div>' );
				wp_nonce_field( 'save', 'everest-forms-coupons_nonce' );
				printf( '</div>' );
		?>
				</form>
			</div>
		<?php
	}

	/**
	 * Inser Coupon.
	 *
	 * @since 1.0.0
	 */
	public function insert_coupon() {
		// Check for non empty $_POST.
		if ( ! isset( $_POST['evf-coupons-nonce'], $_POST['evf-coupon-insert'] ) ) {
			return;
		}

		// Nonce check.
		if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['evf-coupons-nonce'] ) ), 'evf_coupons_nonce' ) ) {
			wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms-pro' ) );
		}
		$data = $this->process_coupon( $_POST );

		if ( 0 >= count( $data['msg'] ) ) {
			$post_id = wp_insert_post(
				array(
					'post_content' => wp_json_encode( $data['data'] ),
					'post_title'   => $data['data']['title'],
					'post_type'    => 'evf_coupons',
					'post_status'  => 'publish',
				)
			);

			if ( $post_id ) {
				add_settings_error(
					'coupon_insert_action',
					'coupon_insert_action',
					esc_html__( 'Coupon created successfully.', 'everest-forms-pro' ),
					'updated'
				);
				$_POST = array();
			} else {
				add_settings_error(
					'coupon_insert_action',
					'coupon_insert_action',
					esc_html__( 'Sorry!, error while creating coupon.', 'everest-forms-pro' ),
					'error'
				);
			}
		} else {

			foreach ( $data['msg'] as $key => $value ) {
				self::evf_coupons_errors()->add( 'evf_coupon_' . $key, $value );
			}
			add_settings_error(
				'coupon_insert_action',
				'coupon_insert_action',
				esc_html__( 'There was an error while saving your coupon.', 'everest-forms-pro' ),
				'error'
			);
		}
	}

	/**
	 * Edit Coupon.
	 *
	 * @since 1.0.0
	 */
	public function update_coupon() {
		// Check for non empty $_POST.
		if ( ! isset( $_POST['evf-coupons-nonce'], $_POST['evf-coupon-update'] ) ) {
			return;
		}

		// Nonce check.
		if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['evf-coupons-nonce'] ) ), 'evf_coupons_nonce' ) ) {
			wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms-pro' ) );
		}
		$post_id = isset( $_POST['evf_coupon_update_id'] ) ? absint( wp_unslash( $_POST['evf_coupon_update_id'] ) ) : 0;
		$data    = $this->process_coupon( $_POST );

		if ( 0 >= count( $data['msg'] ) ) {
			$post_id = wp_update_post(
				array(
					'ID'           => $post_id,
					'post_content' => wp_json_encode( $data['data'] ),
					'post_title'   => $data['data']['title'],
					'post_type'    => 'evf_coupons',
					'post_status'  => 'publish',
				)
			);

			if ( $post_id ) {
				add_settings_error(
					'coupon_update_action',
					'coupon_update_action',
					esc_html__( 'Coupon updated successfully.', 'everest-forms-pro' ),
					'updated'
				);
			} else {
				add_settings_error(
					'coupon_update_action',
					'coupon_update_action',
					esc_html__( 'Sorry!, error while updating coupon.', 'everest-forms-pro' ),
					'error'
				);
			}
		} else {
			foreach ( $data['msg'] as $key => $value ) {
				self::evf_coupons_errors()->add( 'evf_coupon_' . $key, $value );
			}
			add_settings_error(
				'coupon_insert_action',
				'coupon_insert_action',
				esc_html__( 'There was an error while updating your coupon.', 'everest-forms-pro' ),
				'error'
			);
		}
	}

	/**
	 * Validate screen options on update.
	 *
	 * @param bool|int $status Screen option value. Default false to skip.
	 * @param string   $option The option name.
	 * @param int      $value  The number of rows to use.
	 */
	public function set_screen_option( $status, $option, $value ) {
		if ( in_array( $option, array( 'evf_coupons_per_page' ), true ) ) {
			return $value;
		}

		return $status;
	}

	/**
	 *  Remove screen options.
	 */
	public function remove_screen_options() {
		if ( isset( $_REQUEST['create-coupon'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			return false;
		} elseif ( ( isset( $_REQUEST['action'] ) && 'edit' === $_REQUEST['action'] ) && isset( $_REQUEST['coupon'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			return false;
		} else {
			$current_screen = get_current_screen();

			if ( 'everest-forms_page_evf-coupons' === $current_screen->id ) {
				return true;
			}
		}
	}

	/**
	 * Add Coupons add-on screen_ids to the pool of everest forms screen ids.
	 *
	 * @param array $screen_ids Screens ids of everest forms and addons.
	 * @return array
	 */
	public function evf_coupons_add_screen_id( $screen_ids ) {

		$evffl_screen_ids = array(
			'everest-forms_page_evf-coupons',
		);

		$screen_ids = array_merge( $screen_ids, $evffl_screen_ids );
		return $screen_ids;
	}

	/**
	 * Register core post types.
	 */
	public function register_post_types() {
		if ( ! is_blog_installed() || post_type_exists( 'evf_coupons' ) ) {
			return;
		}

		register_post_type(
			'evf_coupons',
			apply_filters(
				'everest_forms_coupons_register_post_type',
				array(
					'labels'              => array(
						'name'                  => __( 'Coupons', 'everest-forms-pro' ),
						'singular_name'         => __( 'Coupon', 'everest-forms-pro' ),
						'all_items'             => __( 'Coupons', 'everest-forms-pro' ),
						'menu_name'             => _x( 'Coupons', 'Admin menu name', 'everest-forms-pro' ),
						'add_new'               => __( 'Add New', 'everest-forms-pro' ),
						'add_new_item'          => __( 'Add a new coupon', 'everest-forms-pro' ),
						'edit'                  => __( 'Edit', 'everest-forms-pro' ),
						'edit_item'             => __( 'Edit coupon', 'everest-forms-pro' ),
						'new_item'              => __( 'New coupon', 'everest-forms-pro' ),
						'view_item'             => __( 'View coupon', 'everest-forms-pro' ),
						'search_items'          => __( 'Search coupons', 'everest-forms-pro' ),
						'not_found'             => __( 'No coupons found', 'everest-forms-pro' ),
						'not_found_in_trash'    => __( 'No coupons found in trash', 'everest-forms-pro' ),
						'parent'                => __( 'Parent coupons', 'everest-forms-pro' ),
						'insert_into_item'      => __( 'Insert into coupon', 'everest-forms-pro' ),
						'filter_items_list'     => __( 'Filter coupons', 'everest-forms-pro' ),
						'items_list_navigation' => __( 'Coupons navigation', 'everest-forms-pro' ),
						'items_list'            => __( 'Coupons list', 'everest-forms-pro' ),
					),
					'public'              => false,
					'show_ui'             => true,
					'description'         => __( 'This is where you can add new coupons.', 'everest-forms-pro' ),
					'capability_type'     => 'post',
					'publicly_queryable'  => false,
					'exclude_from_search' => true,
					'show_in_rest'        => true,
					'show_in_menu'        => false,
					'menu_position'       => 3,
					'hierarchical'        => false,
					'rewrite'             => false,
					'query_var'           => false,
					'supports'            => false,
					'show_in_nav_menus'   => false,
					'show_in_admin_bar'   => false,
				)
			)
		);
	}

	/**
	 * Process coupon.
	 *
	 * @param array $data Request Data.
	 * @since 1.0.0
	 */
	public function process_coupon( $data ) {
		$processed_data = array();
		$messages       = array();

		$title         = isset( $data['coupons']['title'] ) && ! empty( $data['coupons']['title'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['title'] ) ) : '';
		$prefix        = isset( $data['coupons']['prefix'] ) && ! empty( $data['coupons']['prefix'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['prefix'] ) ) : '';
		$coupon_number = isset( $data['coupons']['number'] ) && ! empty( $data['coupons']['prefix'] ) ? absint( sanitize_text_field( wp_unslash( $data['coupons']['prefix'] ) ) ) : 0;

		if ( ! is_numeric( $coupon_number ) ) {
			$messages['number'] = __( 'Please enter valid number', 'everest-forms-pro' );
		}

		if ( isset( $data['coupons']['prefix'] ) && ! empty( $data['coupons']['prefix'] ) && '' === $title ) {
			if ( preg_match( '/[^a-z0-9]/i', $prefix ) ) {
				$messages['prefix'] = __( 'Please enter a valid Coupon Code. The Coupon Code can only contain alphanumeric characters.', 'everest-forms-pro' );
			}

			$coupon_code             = strtoupper( $prefix . evf_get_random_string( 10 ) );
			$processed_data['title'] = $coupon_code;
		} elseif ( '' === $title && '' === $prefix ) {
			$messages['title'] = __( 'This field is required.', 'everest-forms-pro' );
		} else {
			$processed_data['title'] = $title;
		}

		$code = isset( $data['coupons']['code'] ) && ! empty( $data['coupons']['code'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['code'] ) ) : '';

		if ( '' === $code && '' === $prefix ) {
			$messages['code'] = __( 'This field is required.', 'everest-forms-pro' );
		} elseif ( ! empty( $prefix ) ) {
			$processed_data['code'] = $coupon_code;
		} elseif ( preg_match( '/[^a-z0-9]/i', $code ) ) {
			$messages['code'] = __( 'Please enter a valid Coupon Code. The Coupon Code can only contain alphanumeric characters.', 'everest-forms-pro' );
		} else {
			$processed_data['code'] = $code;
		}

		$type                   = isset( $data['coupons']['discount_type'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['discount_type'] ) ) : 'fixed';
		$processed_data['type'] = $type;

		$amount = isset( $data['coupons']['discount_amount'] ) && ! empty( $data['coupons']['discount_amount'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['discount_amount'] ) ) : '';

		if ( '' === $amount ) {
			$messages['amount'] = __( 'This field is required.', 'everest-forms-pro' );
		} else {
			$currency   = get_option( 'everest_forms_currency', 'USD' );
			$currencies = evf_get_currencies();

			if ( 'fixed' === $type ) {
				$formatted_amount = evf_format_amount( evf_sanitize_amount( $amount ) );

				if ( 0 < absint( $formatted_amount ) ) {
					$processed_data['amount'] = sanitize_text_field( $currencies[ $currency ]['symbol'] ) . $formatted_amount;
				} else {
					$messages['amount'] = __( 'Discount amount should be valid amount.', 'everest-forms-pro' );
				}
			} else {
				$sanitized_percent = floatval( preg_replace( '/\.(([^\.]*)\.)*/', '.$2', preg_replace( '/[^\d\.]/', '', $amount ) ) );

				if ( 0 < absint( $sanitized_percent ) ) {

					if ( preg_match( '/\./', $sanitized_percent ) && 2 < strlen( explode( '.', $sanitized_percent )[1] ) ) {
						$processed_data['amount'] = number_format( $sanitized_percent, 2 ) . '%';
					} else {
						$processed_data['amount'] = $sanitized_percent . '%';
					}
				} else {
					$messages['amount'] = __( 'Discount percent should be valid number.', 'everest-forms-pro' );
				}
			}
		}

		$start_date = isset( $data['coupons']['start_date'] ) && ! empty( $data['coupons']['start_date'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['start_date'] ) ) : '';
		if ( '' === $start_date ) {
			$messages['start_date'] = __( 'This field is required.', 'everest-forms-pro' );
		} else {
			$processed_data['start_date'] = $start_date;
		}

		$end_date = isset( $data['coupons']['end_date'] ) && ! empty( $data['coupons']['end_date'] ) ? sanitize_text_field( wp_unslash( $data['coupons']['end_date'] ) ) : '';
		if ( '' === $end_date ) {
			$messages['end_date'] = __( 'This field is required.', 'everest-forms-pro' );
		} else {
			$processed_data['end_date'] = $end_date;
		}

		if ( strtotime( $start_date ) > strtotime( $end_date ) ) {
			$messages['start_date'] = __( 'Start date can not be greater than end date.', 'everest-forms-pro' );
		}

		$applicable_form_ids = isset( $data['coupons']['applicable_form_ids'] ) && ! empty( $data['coupons']['applicable_form_ids'] ) ? wp_unslash( $data['coupons']['applicable_form_ids'] ) : array();

		foreach ( $applicable_form_ids as $value ) {
			$processed_data['applicable_forms'][ absint( $value ) ] = absint( $value );
		}

		if ( isset( $data['coupons']['status'] ) ) {
			$today       = date_i18n( 'Y-m-d', strtotime( 'today' ) );
			$expiry_date = date_i18n( 'Y-m-d', strtotime( $end_date ) );

			if ( 'active' === $data['coupons']['status'] && strtotime( $expiry_date ) < strtotime( $today ) ) {
				$messages['status'] = __( 'Status can not be Active while End Date is less than Today Date.', 'everest-forms-pro' );
			}
		}

		$processed_data['status'] = isset( $data['coupons']['status'] ) && 'active' === $data['coupons']['status'] ? '1' : '0';

		return array(
			'data' => $processed_data,
			'msg'  => $messages,
		);
	}

	/**
	 * Used for tracking error messages.
	 *
	 * @since 1.0.0
	 */
	public static function evf_coupons_errors() {
		static $wp_error;
		$wp_error = isset( $wp_error ) ? $wp_error : new \WP_Error( null, null, null );
		return $wp_error;
	}

	/**
	 * Get Coupons.
	 *
	 * @return array Coupons.
	 */
	public static function get_coupons() {
		// Query args.
		$args = array(
			'post_type'           => 'evf_coupons',
			'ignore_sticky_posts' => true,
			'post_status'         => array( 'any' ),
		);
		// Get the forms.
		$query = new \WP_Query( $args );
		// Set the pagination.
		$coupons = array();
		foreach ( $query->posts as $key => $post ) {
			$coupon = ! empty( $post->post_content ) ? evf_decode( $post->post_content ) : '';
			if ( '' !== $coupon ) {
				$coupon['id'] = $post->ID;
				$coupons[]    = $coupon;
			}
		}
		return $coupons;
	}

	/**
	 * Generates the bulk coupon code.
	 *
	 * @since 3.0.5
	 */
	public function generate_bulk_coupon_code() {
		if ( ! isset( $_POST['evf-bulk-coupon'] ) || $_POST['evf-bulk-coupon'] !== 'Save' ) {
			return false;
		}

		if ( ! wp_verify_nonce( sanitize_key( $_POST['evf-coupons-nonce'] ), 'evf_coupons_nonce' ) ) {
			wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms-pro' ) );
		}

		$coupon_number = isset( $_POST['coupons']['number'] ) ? intval( $_POST['coupons']['number'] ) : 0;

		if ( self::$background_process->is_updating() ) {
			add_settings_error(
				'coupon_insert_action',
				'coupon_insert_action',
				esc_html__( 'A background process is already running. Please wait until it completes.', 'everest-forms-pro' ),
				'error'
			);
			return false;
		}

		$errors = [];

		for ( $i = 0; $i <= $coupon_number; $i++ ) {
			$data = $this->process_coupon( $_POST );

			if ( empty( $data['msg'] ) ) {
				self::$background_process->push_to_queue( array( 'data' => $data['data'] ) );
			} else {
				foreach ( $data['msg'] as $key => $value ) {
					self::evf_coupons_errors()->add( 'evf_coupon_' . $key, $value );
					$errors[] = $value;
				}
			}
		}

		if ( ! empty( $errors ) ) {
			add_settings_error(
				'coupon_insert_action',
				'coupon_insert_action',
				esc_html__( 'There is an error while creating coupons. Please see the errors below.', 'everest-forms-pro' ) . $errors[0],
				'error'
			);
		}

		self::$background_process->save()->dispatch();
	}
}
