<?php
/**
 * Abandoned cart class.
 *
 * @package xts
 */

namespace XTS\Modules;

use XTS\Framework\Options;
use XTS\Framework\Module;

/**
 * Abandoned cart class.
 */
class WC_Abandoned_Cart extends Module {

	/**
	 * Init.
	 */
	public function init() {
		$this->define_constants();
		add_action( 'init', array( $this, 'hooks' ) );
	}

	/**
	 * Define constants.
	 *
	 * @since 1.0.0
	 */
	private function define_constants() {
		if ( ! defined( 'XTS_ABANDONED_CART_DIR' ) ) {
			define( 'XTS_ABANDONED_CART_DIR', XTS_FRAMEWORK_ABSPATH . '/modules/wc-abandoned-cart/' );
		}
	}

	/**
	 * Init hooks.
	 */
	public function hooks() {
		$this->add_options();

		$this->include_files();
	}

	/**
	 * Add options in theme settings.
	 *
	 * @return void
	 */
	public function add_options() {

		Options::add_section(
			array(
				'id'       => 'abandoned_cart_section',
				'name'     => esc_html__( 'Abandoned cart', 'xts-theme' ),
				'priority' => 110,
				'parent'   => 'shop_section',
				'icon'     => 'xf-shop',
			)
		);

		Options::add_field(
			array(
				'id'          => 'cart_recovery_enabled',
				'name'        => esc_html__( 'Enable cart recovery', 'xts-theme' ),
				'description' => esc_html__( 'Reminds customers via email about items left in their cart, boosting sales by recovering potential lost purchases.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => false,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 10,
			)
		);

		Options::add_field(
			array(
				'id'          => 'recover_guest_cart_enabled',
				'name'        => esc_html__( 'Recover guest carts', 'xts-theme' ),
				'description' => esc_html__( 'Saves the cart of an unregistered user if they provide their email at checkout.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => false,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 20,
			)
		);

		Options::add_field(
			array(
				'id'          => 'recover_guest_cart_enable_privacy_checkbox',
				'name'        => esc_html__( 'Guest data consent', 'xts-theme' ),
				'description' => esc_html__( 'Adds a checkbox for guest users to consent to data storage, enabling abandoned cart email reminder.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => false,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 30,
				'requires'    => array(
					array(
						'key'     => 'recover_guest_cart_enabled',
						'compare' => 'equals',
						'value'   => true,
					),
				),
			)
		);

		Options::add_field(
			array(
				'id'       => 'recover_guest_cart_privacy_checkbox_text',
				'type'     => 'textarea',
				'name'     => esc_html__( 'Guest data consent text', 'xts-theme' ),
				'wysiwyg'  => false,
				'section'  => 'abandoned_cart_section',
				'default'  => esc_html__( "If you check this box, you are giving us permission to save some of your details into a contact list. You may receive email messages containing information of commercial or promotional nature concerning this store.\nPersonal Data collected: email address, first name and last name.", 'xts-theme' ),
				'requires' => array(
					array(
						'key'     => 'recover_guest_cart_enabled',
						'compare' => 'equals',
						'value'   => true,
					),
					array(
						'key'     => 'recover_guest_cart_enable_privacy_checkbox',
						'compare' => 'equals',
						'value'   => true,
					),
				),
				'priority' => 40,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_timeframe',
				'type'        => 'text_input',
				'name'        => esc_html__( 'Cart timeout', 'xts-theme' ),
				'description' => esc_html__( 'Sets when an inactive cart is marked as abandoned.', 'xts-theme' ),
				'section'     => 'abandoned_cart_section',
				'attributes'  => array(
					'type' => 'number',
					'min'  => 1,
				),
				'class'       => 'xts-col-6',
				'priority'    => 50,
				'default'     => 2,
			)
		);

		Options::add_field(
			array(
				'id'       => 'abandoned_cart_timeframe_period',
				'type'     => 'select',
				'name'     => esc_html__( 'Time unit', 'xts-theme' ),
				'section'  => 'abandoned_cart_section',
				'options'  => array(
					strval( MINUTE_IN_SECONDS ) => array(
						'name'  => esc_html__( 'Minutes', 'xts-theme' ),
						'value' => strval( MINUTE_IN_SECONDS ),
					),
					strval( HOUR_IN_SECONDS )   => array(
						'name'  => esc_html__( 'Hours', 'xts-theme' ),
						'value' => strval( HOUR_IN_SECONDS ),
					),
					strval( DAY_IN_SECONDS )    => array(
						'name'  => esc_html__( 'Days', 'xts-theme' ),
						'value' => strval( DAY_IN_SECONDS ),
					),
				),
				'default'  => strval( DAY_IN_SECONDS ),
				'class'    => 'xts-col-6',
				'priority' => 60,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_delete_timeframe',
				'name'        => esc_html__( 'Cart cleanup', 'xts-theme' ),
				'description' => esc_html__( 'Automatically removes abandoned carts after the specified time.', 'xts-theme' ),
				'type'        => 'text_input',
				'section'     => 'abandoned_cart_section',
				'attributes'  => array(
					'type' => 'number',
					'min'  => 1,
				),
				'priority'    => 70,
				'class'       => 'xts-col-6',
				'default'     => 30,
			)
		);
		Options::add_field(
			array(
				'id'       => 'abandoned_cart_delete_timeframe_period',
				'name'     => esc_html__( 'Time unit', 'xts-theme' ),
				'type'     => 'select',
				'section'  => 'abandoned_cart_section',
				'options'  => array(
					strval( MINUTE_IN_SECONDS ) => array(
						'name'  => esc_html__( 'Minutes', 'xts-theme' ),
						'value' => strval( MINUTE_IN_SECONDS ),
					),
					strval( HOUR_IN_SECONDS )   => array(
						'name'  => esc_html__( 'Hours', 'xts-theme' ),
						'value' => strval( HOUR_IN_SECONDS ),
					),
					strval( DAY_IN_SECONDS )    => array(
						'name'  => esc_html__( 'Days', 'xts-theme' ),
						'value' => strval( DAY_IN_SECONDS ),
					),
				),
				'default'  => strval( DAY_IN_SECONDS ),
				'class'    => 'xts-col-6',
				'priority' => 80,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_coupon_enabled',
				'name'        => esc_html__( 'Enable coupon', 'xts-theme' ),
				'description' => esc_html__( 'Activate this option to include a discount coupon in the email.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => false,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 90,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_coupon_prefix',
				'name'        => esc_html__( 'Coupon prefix', 'xts-theme' ),
				'description' => esc_html__( 'Specify a prefix to easily identify coupons used for cart recovery emails.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'type'        => 'text_input',
				'section'     => 'abandoned_cart_section',
				'default'     => 'XTS',
				'priority'    => 100,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_coupon_amount',
				'name'        => esc_html__( 'Coupon amount', 'xts-theme' ),
				'description' => esc_html__( 'Set the coupon value and select discount type.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'type'        => 'text_input',
				'section'     => 'abandoned_cart_section',
				'attributes'  => array(
					'type' => 'number',
					'min'  => 1,
				),
				'class'       => 'xts-col-6',
				'default'     => 10,
				'priority'    => 110,
			)
		);

		Options::add_field(
			array(
				'id'       => 'abandoned_cart_coupon_discount_type',
				'group'    => esc_html__( 'Coupon', 'xts-theme' ),
				'name'     => esc_html__( 'Discount type', 'xts-theme' ),
				'section'  => 'abandoned_cart_section',
				'type'     => 'select',
				'options'  => array(
					'percent'    => array(
						'name'  => esc_html__( 'Percentage', 'xts-theme' ),
						'value' => 'percent',
					),
					'fixed_cart' => array(
						'name'  => esc_html__( 'Fixed', 'xts-theme' ),
						'value' => 'fixed_cart',
					),
				),
				'class'    => 'xts-col-6',
				'default'  => 'percent',
				'priority' => 120,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_delete_used_coupons',
				'name'        => esc_html__( 'Delete used coupons', 'xts-theme' ),
				'description' => esc_html__( 'Activate this setting to automatically remove coupons after they\'ve been redeemed.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => true,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 130,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_delete_expired_coupons',
				'name'        => esc_html__( 'Delete expired coupons', 'xts-theme' ),
				'description' => esc_html__( 'Activate this option to remove expired coupons automatically.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'abandoned_cart_section',
				'default'     => true,
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 140,
			)
		);

		Options::add_field(
			array(
				'id'          => 'abandoned_cart_coupon_timeframe',
				'name'        => esc_html__( 'Coupon expires after', 'xts-theme' ),
				'description' => esc_html__( 'Set the number of days until the coupon expires.', 'xts-theme' ),
				'group'       => esc_html__( 'Coupon', 'xts-theme' ),
				'section'     => 'abandoned_cart_section',
				'type'        => 'text_input',
				'attributes'  => array(
					'type' => 'number',
					'min'  => 1,
				),
				'class'       => 'xts-col-6',
				'requires'    => array(
					array(
						'key'     => 'abandoned_cart_delete_expired_coupons',
						'compare' => 'equals',
						'value'   => true,
					),
				),
				'priority'    => 150,
				'default'     => 1,
			),
		);

		Options::add_field(
			array(
				'id'       => 'abandoned_cart_coupon_timeframe_period',
				'name'     => esc_html__( 'Time unit', 'xts-theme' ),
				'group'    => esc_html__( 'Coupon', 'xts-theme' ),
				'type'     => 'select',
				'section'  => 'abandoned_cart_section',
				'options'  => array(
					strval( DAY_IN_SECONDS )   => array(
						'name'  => esc_html__( 'Days', 'xts-theme' ),
						'value' => strval( DAY_IN_SECONDS ),
					),
					strval( WEEK_IN_SECONDS )  => array(
						'name'  => esc_html__( 'Weeks', 'xts-theme' ),
						'value' => strval( WEEK_IN_SECONDS ),
					),
					strval( MONTH_IN_SECONDS ) => array(
						'name'  => esc_html__( 'Months', 'xts-theme' ),
						'value' => strval( MONTH_IN_SECONDS ),
					),
				),
				'requires' => array(
					array(
						'key'     => 'abandoned_cart_delete_expired_coupons',
						'compare' => 'equals',
						'value'   => true,
					),
				),
				'class'    => 'xts-col-6',
				'default'  => strval( DAY_IN_SECONDS ),
				'priority' => 160,
			),
		);
	}

	/**
	 * Get list of module include files.
	 *
	 * @return void
	 */
	protected function include_files() {
		$files = array();

		if ( ! class_exists( 'WP_List_Table' ) ) {
			include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
		}

		$files = array_merge(
			$files,
			array(
				'class-manager',
				'class-admin',
				'class-emails',
				'list-tables/class-abandoned-cart-table',
				'list-tables/class-cart-content-table',
			)
		);

		foreach ( $files as $file ) {
			$path = XTS_ABANDONED_CART_DIR . $file . '.php';
			if ( file_exists( $path ) ) {
				require_once $path;
			}
		}
	}

	/**
	 * Get post type arguments.
	 *
	 * @return array
	 */
	public function get_post_type_args() {
		return array(
			'label'               => esc_html__( 'Carts', 'xts-theme' ),
			'labels'              => array(
				'name'          => esc_html__( 'Abandoned Cart', 'xts-theme' ),
				'singular_name' => esc_html__( 'Abandoned Cart', 'xts-theme' ),
				'menu_name'     => esc_html__( 'Abandoned Cart', 'xts-theme' ),
				'add_new'       => esc_html__( 'Add New Abandoned Cart', 'xts-theme' ),
				'add_new_item'  => esc_html__( 'Add New Abandoned Cart', 'xts-theme' ),
				'edit_item'     => esc_html__( 'Abandoned cart information', 'xts-theme' ),
			),
			'supports'            => array( '' ),
			'show_in_nav_menus'   => false,
			'hierarchical'        => false,
			'public'              => false,
			'show_ui'             => true,
			'show_in_menu'        => false,
			'exclude_from_search' => true,
			'capability_type'     => 'post',
			'capabilities'        => array( 'create_posts' => false ),
			'map_meta_cap'        => true,
		);
	}
}
