<?php
/**
 * A class for generating the orderpage for one or more (combined) events.
 *
 * @since 1.0
 */

declare( strict_types=1 );
namespace FeData\FastEvents\Hooks\Shortcodes;

defined( 'ABSPATH' ) || exit;

use FeData\FastEvents\Helpers\EventsHelper;
use FeData\FastEvents\Logging\LoggerFactory;
use FeData\FastEvents\Utilities\PluginUtil;
use FeData\FastEvents\Utilities\TimeUtil;


/**
 * A class for generating the orderpage for one or more (combined) events.
 */
final class OrderPageShortcode {

	/**
	 * OrderPageShortcode constructor.
	 *
	 * @since 3.0.0
	 *
	 * @param LoggerFactory $logger        Logger.
	 * @param EventsHelper  $events_helper Event helper functions.
	 */
	public function __construct( private readonly LoggerFactory $logger, private readonly EventsHelper $events_helper ) {
	}


	/**
	 * Generate the orderpage for this event
	 *
	 * @since 1.0
	 * @since 2.2.0 Add coupons_needed to query
	 *
	 * @param array $atts   Shortcode attributes ('id' or 'group').
	 *
	 * @return false|string
	 */
	public function create_page( array $atts ): bool|string {
		global $wpdb;

		wp_enqueue_style( 'FE-Events-styles', PluginUtil::plugin_url() . '/assets/css/fast-events.min.css', null, $GLOBALS['fast_events']::FAST_EVENTS_VERSION, false );

		wp_register_script( 'FE-payment-js', PluginUtil::plugin_url() . '/assets/js/fe-payment.min.js', null, $GLOBALS['fast_events']::FAST_EVENTS_VERSION, true );
		wp_enqueue_script( 'FE-payment-js' );

		// Is it a single event sale?
		if ( isset( $atts['id'] ) ) {

			$result = $wpdb->get_results(
				$wpdb->prepare(
					'SELECT id, event_name, event_date, start_date, end_date, stock, sold, stock_link, event_group, group_type, ' .
					"event_type, input_fields, recaptcha_needed, coupons_needed, terms FROM {$wpdb->prefix}fe_events WHERE id = %d",
					absint( $atts['id'] )
				)
			);

			// Does the event exist?
			if ( empty( $result ) ) {
				wp_die( esc_html__( 'Event does not exist', 'fast-events' ) );
			}

			// Is the event already active?
			if ( TimeUtil::local_time() < TimeUtil::date_local_time( $result[0]->start_date ) ) {
				return '<div class="fast-event-event-not-active">' . esc_html__( 'The sale starts on: ', 'fast-events' ) . $result[0]->start_date . '<br></div>';
			}

			// Is the event still active?
			if ( TimeUtil::local_time() > TimeUtil::date_local_time( $result[0]->end_date ) ) {
				return '<div class="fast-event-event-not-active">' . esc_html__( 'The sale stopped on: ', 'fast-events' ) . $result[0]->end_date . '<br></div>';
			}

			// Is there stock control and is the event sold out?
			if ( false === $this->events_helper->check_stock( $result[0] ) ) {
				return '<div class="fast-event-event-sold-out">' . esc_html__( 'The event is sold out', 'fast-events' ) . '</div>';
			}

			// Get the order screen.
			return $this->events_helper->get_order_form( $result, $this->events_helper::FAST_EVENTS_CLIENT );

		} elseif ( isset( $atts['group'] ) ) {

			$result = $wpdb->get_results(
				$wpdb->prepare(
					'SELECT id, event_name, event_date, start_date, end_date, stock, sold, stock_link, event_group, group_type, ' .
					"event_type, input_fields, recaptcha_needed, coupons_needed, terms FROM {$wpdb->prefix}fe_events " .
					'WHERE event_group = %s AND group_type IN (1,2) ORDER BY event_name ASC',
					sanitize_text_field( $atts['group'] )
				)
			);

			if ( null === $result || 0 === count( $result ) ) {
				wp_die( esc_html__( 'Event-group does not exist', 'fast-events' ) . ': ' . esc_html( $wpdb->last_error ) );
			}

			// Is the event already active?
			if ( TimeUtil::local_time() < TimeUtil::date_local_time( $result[0]->start_date ) ) {
				return '<div class="fast-event-event-not-active">' . esc_html__( 'The sale starts on: ', 'fast-events' ) . $result[0]->start_date . '<br></div>';
			}

			// Is the event still active?
			if ( TimeUtil::local_time() > TimeUtil::date_local_time( $result[0]->end_date ) ) {
				return '<div class="fast-event-event-not-active">' . esc_html__( 'The sale stopped on: ', 'fast-events' ) . $result[0]->end_date . '<br></div>';
			}

			// Check if a group of events is sold out.
			$sold_out = false;
			foreach ( $result as $item ) {
				if ( true === $this->events_helper->check_stock( $item ) ) {
					$sold_out = true;
				}
			}
			if ( false === $sold_out ) {
				return '<div class="fast-event-event-sold-out">' . esc_html__( 'The event is sold out', 'fast-events' ) . '</div>';
			}

			// Now that we are dealing with a group of events, we assume they all have the same sell-dates.
			// Get the first one to generate the form.
			return $this->events_helper->get_order_form( $result, $this->events_helper::FAST_EVENTS_CLIENT );

		} else {
			// Wrong shortcode.
			wp_die( esc_html__( 'Event does not exist', 'fast-events' ) );
		}
	}
}
