<?php
/**
 *  The admin offer banner.
 *
 * @since        2.1.11
 * @version      2.1.11
 *
 * @package    eventful-for-elementor
 * @subpackage eventful-for-elementor/src/Admin/Helpers
 * @author     ThemeAtelier<themeatelierbd@gmail.com>
 */


namespace ThemeAtelier\EventfulForElementor\Admin\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * The class for all admin notices on the backend.
 */
class ThemeAtelier_Offer_Banner {

	/**
	 * The single instance of the class.
	 *
	 * @var self
	 * @since
	 */
	private static $instance = null;

	/**
	 * Class constructor.
	 */
	private function __construct() {
		add_action( 'admin_notices', array( $this, 'render_offer_banner' ) );
		add_action( 'wp_ajax_themeatelier_dismiss_offer_banner', array( $this, 'dismiss_offer_banner' ) );
	}

	/**
	 * Retrieves the singleton instance of the class.
	 *
	 * This method ensures that only one instance of the class is created (singleton pattern).
	 *
	 * @return self The singleton instance of the class.
	 */
	public static function instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Retrieves the active offers.
	 *
	 * @return array The list of active offers.
	 */
	public function get_active_offers() {
		$now = time();

		// Define offer durations.
		$offers = array(
			'new_year'     => array(
				'id'    => 'new_year_2026',
				'start' => strtotime( '2025-12-20 00:00:00' ),
				'end'   => strtotime( '2026-01-15 23:59:59' ),
				'image' => EVENTFUL_ELEMENTOR_PLUGIN_URL . '/src/Admin/HelpPage/assets/images/eventful-el-ny-banner.svg',
				'link'  => 'https://themeatelier.net/downloads/eventful-for-elementor/?utm_source=eventful_for_elementor&utm_medium=offer_banner&utm_campaign=new_year_2026#pricing',
			),
			'spring_sale'     => array(
				'id'    => 'spring_2026',
				'start' => strtotime( '2026-04-01 00:00:00' ),
				'end'   => strtotime( '2026-04-30 23:59:59' ),
				'image' => EVENTFUL_ELEMENTOR_PLUGIN_URL . '/src/Admin/HelpPage/assets/images/eventful-for-elementor-spring.svg',
				'link'  => 'https://themeatelier.net/downloads/eventful-for-elementor/?utm_source=eventful_for_elementor&utm_medium=offer_banner&utm_campaign=spring_2026#pricing',
			),
			'growth_sale'     => array(
				'id'    => 'may_2026',
				'start' => strtotime( '2026-04-01 00:00:00' ),
				'end'   => strtotime( '2026-04-30 23:59:59' ),
				'image' => EVENTFUL_ELEMENTOR_PLUGIN_URL . '/src/Admin/HelpPage/assets/images/may-growth-sale.svg',
				'link'  => 'https://themeatelier.net/downloads/eventful-for-elementor/?utm_source=eventful_for_elementor&utm_medium=offer_banner&utm_campaign=may_2026#pricing',
			),
		);


		$active_offers = array();

		foreach ( $offers as $key => $offer ) {
			if ( $now >= $offer['start'] && $now <= $offer['end'] ) {
				$active_offers[ $key ] = $offer;
			}
		}

		return $active_offers;
	}

	/**
	 * Renders the offer banner on the page.
	 *
	 * @return void
	 */
	public function render_offer_banner() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$active_offers = $this->get_active_offers();

		if ( empty( $active_offers ) ) {
			return;
		}

		foreach ( $active_offers as $offer ) {
			$option_key = 'themeatelier_offer_banner_dismissed_' . $offer['id'];

			// Check dismissed status.
			if ( get_option( $option_key ) ) {
				continue;
			}

			$nonce = wp_create_nonce( 'domain_for_sale_offer_dismiss' );
			?>
			<div id="themeatelier-offer-banner" class="notice notice-info is-dismissible">
				<a href="<?php echo esc_url( $offer['link'] ); ?>" target="_blank">
					<img src="<?php echo esc_url( $offer['image'] ); ?>" alt="ThemeAtelier Offer" style="width:100%;height:auto;">
				</a>
			
				<button type="button"
					class="notice-dismiss themeatelier-offer-banner-dismiss"
					data-offer-id="<?php echo esc_attr( $offer['id'] ); ?>"
					data-nonce="<?php echo esc_attr( $nonce ); ?>">
				</button>
			</div>
				<?php
		}
		?>
		<script type="text/javascript">
			(function($){
				$(document).on('click', '#themeatelier-offer-banner .notice-dismiss', function(e){
					e.preventDefault();
					const nonce = $(this).data('nonce');
					const offerID = $(this).data('offer-id')
					$.post(ajaxurl, {
						action: 'themeatelier_dismiss_offer_banner',
						offer_id: offerID,
						nonce: nonce
					});
					$('#themeatelier-offer-banner').fadeOut(300);
				});
			})(jQuery);
		</script>
		<?php
	}

	/**
	 * Handles the AJAX request to dismiss the offer banner.
	 *
	 * @return void
	 */
	public function dismiss_offer_banner() {
		check_ajax_referer( 'domain_for_sale_offer_dismiss', 'nonce' );
		$offer_id = isset( $_POST['offer_id'] ) ? sanitize_text_field( wp_unslash( $_POST['offer_id'] ) ) : '';

		update_option( 'themeatelier_offer_banner_dismissed_' . $offer_id, true );

		wp_send_json_success();
	}
}