<?php
/**
 * Price tracker class.
 *
 * @package xts
 */

namespace XTS\Modules;

use XTS\Framework\Options;
use XTS\Modules\Managers\Module_Endpoints_Manager;
use XTS\Framework\Module;

/**
 * Price tracker class.
 */
class Wc_Price_Tracker extends Module {
	/**
	 * Init.
	 */
	public function init() {
		$this->define_constants();
		add_action( 'init', array( $this, 'hooks' ) );
	}

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

		$this->include_files();

		$this->add_endpoint_options();
	}

	/**
	 * Define constants.
	 */
	private function define_constants() {
		if ( ! defined( 'XTS_PRICE_TRACKER_DIR' ) ) {
			define( 'XTS_PRICE_TRACKER_DIR', XTS_FRAMEWORK_ABSPATH . '/modules/wc-price-tracker/' );
		}
	}

	/**
	 * Add endpoint for price tracker.
	 */
	public function add_endpoint_options() {
		if ( ! xts_get_opt( 'price_tracker_enabled' ) || ! xts_is_woocommerce_installed() ) {
			return;
		}

		$endpoints_manager = Module_Endpoints_Manager::get_instance();

		$endpoints_manager->add_endpoint_options(
			array(
				'title'    => esc_html__( 'Price tracker', 'xts-theme' ),
				'desc'     => esc_html__( 'Endpoint for the "My account &rarr; Price tracker" page.', 'xts-theme' ),
				'id'       => 'xts_myaccount_price_tracker_endpoint',
				'type'     => 'text',
				'default'  => 'price-tracker',
				'desc_tip' => true,
				'priority' => 20,
			)
		);
	}

	/**
	 * Add options in theme settings.F
	 */
	public function add_options() {
		Options::add_section(
			array(
				'id'       => 'price_tracker_section',
				'parent'   => 'shop_section',
				'name'     => esc_html__( 'Price tracker', 'xts-theme' ),
				'priority' => 120,
				'icon'     => 'xf-shop',
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_enabled',
				'name'        => esc_html__( 'Enable "Price tracker"', 'xts-theme' ),
				'description' => esc_html__( 'Enable this option to allow customers to subscribe for price drop notifications on products.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '0',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 10,
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_for_loggined',
				'name'        => esc_html__( 'Login to see price tracker', 'xts-theme' ),
				'description' => esc_html__( 'Restrict the "Price tracker" feature to registered users to ensure that only registered customers can sign up for notifications about price drops on products.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '0',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 20,
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_use_loggedin_email',
				'name'        => esc_html__( 'Skip email input for logged-in users', 'xts-theme' ),
				'description' => esc_html__( 'Hides the email field for authenticated users and uses their account email for notifications.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '0',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 30,
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_desired_price',
				'name'        => esc_html__( 'Enable desired price', 'xts-theme' ),
				'description' => esc_html__( 'Allows the user to specify the exact price after a drop at which they will receive a notification.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '0',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 40,
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_enable_privacy_checkbox',
				'name'        => esc_html__( 'Enable privacy policy checkbox', 'xts-theme' ),
				'description' => esc_html__( 'Activate this setting to require customers to agree to your privacy policy with a checkbox before they can join the price tracker.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '1',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 50,
			)
		);

		Options::add_field(
			array(
				'id'          => 'price_tracker_fragments_enable',
				'name'        => esc_html__( 'Enable fragments updating', 'xts-theme' ),
				'description' => esc_html__( 'Update the price drop notification form dynamically on product pages, ensuring it displays accurate information even when page caching is active.', 'xts-theme' ),
				'type'        => 'switcher',
				'section'     => 'price_tracker_section',
				'default'     => '0',
				'on-text'     => esc_html__( 'Yes', 'xts-theme' ),
				'off-text'    => esc_html__( 'No', 'xts-theme' ),
				'priority'    => 60,
			)
		);
	}

	/**
	 * Get list of module include files.
	 *
	 * @return void
	 */
	protected function include_files() {
		if ( ! xts_get_opt( 'price_tracker_enabled' ) || ! xts_is_woocommerce_installed() ) {
			return;
		}

		$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-db-storage',
				'class-admin',
				'class-frontend',
				'class-emails',
				'list-tables/class-products-table',
				'list-tables/class-users-table',
			)
		);

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