<?php
/**
 * Trip Handler Class.
 */
namespace WPTravelEngine\Posttype;

use WP_Post;
use WPTravelEngine\Core\Models\Post\Trip as PostTrip;

/**
 * @since 6.7.10 - Reverted deprecated label ( deprecated 6.0.0 ); identified as a crucial element for native themes.
 */
#[\AllowDynamicProperties]
class Trip {

	/**
	 * Class instance holder.
	 *
	 * @var array<int, self>
	 */
	protected static $instance = array();

	/**
	 * WP post Object.
	 *
	 * @var ?WP_Post
	 */
	public $post = null;

	/**
	 * Trip Packages post objects.
	 *
	 * @var array
	 */
	public $packages = array();

	/**
	 * PostTrip model instance.
	 *
	 * @var ?PostTrip
	 * @since 6.7.10
	 */
	private ?PostTrip $trip_model = null;

	public function __construct( $post ) {
		// wptravelengine_deprecated_class( __CLASS__, '6.0.0', \WPTravelEngine\Core\Models\Post\Trip::class );
		// add_action( 'wp', array( $this, 'initialize' ) );
		$this->post = $post;

		$this->initialize();
	}

	public function initialize() {

		$this->trip_model      = wptravelengine_get_trip( $this->post );
		$this->trip_version    = $this->trip_model ? $this->trip_model->version() : '1.0.0';
		$this->use_legacy_trip = defined( 'USE_WTE_LEGACY_VERSION' ) && USE_WTE_LEGACY_VERSION;

		$this->set_packages();
		$this->set_default_package();
	}

	public function set_packages() {

		if ( ! $this->trip_model instanceof PostTrip ) {
			return;
		}

		$pkgs = $this->trip_model->packages()->array();

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

		$this->packages = array_column( $pkgs, 'post', 'ID' );
	}

	public function set_default_package() {
		if ( $this->trip_model instanceof PostTrip ) {
			$default_package = $this->trip_model->default_package();
		} else {
			$default_package = null;
		}
		$this->has_sale        = $default_package->{'has_sale'} ?? false;
		$this->price           = $default_package->{'price'} ?? 0;
		$this->sale_price      = $default_package->{'sale_price'} ?? 0;
		$this->sale_percentage = $default_package->{'sale_percentage'} ?? 0;
		$this->default_package = $default_package->post ?? false;
	}

	public function __isset( $key ) {
		return isset( $this->{$key} );
	}

	public function __get( $key ) {

		if ( $this->__isset( $key ) ) {
			return $this->{$key};
		}
		switch ( $key ) {
			case 'has_group_discount':
				return \apply_filters( 'has_packages_group_discounts', false, $this->post->ID );
			default:
				return \get_post_meta( $this->post->ID, $key, true );
		}
	}

	public function has_group_discount() {
		$packages = $this->packages;

		$primary_pricing_category_id = get_option( 'primary_pricing_category', 0 );

		if ( $primary_pricing_category_id ) {
			$term = get_term( $primary_pricing_category_id );
		}
		foreach ( $packages as $package ) {
			$package_categories = (object) $package->{'package-categories'};

			$package_categories_ids = ( isset( $package_categories->{'c_ids'} ) ) ? $package_categories->{'c_ids'} : array();

			if ( ! $primary_pricing_category_id ) {
				$primary_pricing_category_id = ! empty( $package_categories_ids ) && is_array( $package_categories_ids ) ? array_shift( $package_categories_ids ) : 0;
			}
			if ( ! $primary_pricing_category_id ) {
				return false;
			}

			$term = get_term( $primary_pricing_category_id );

			if ( ! ( $term instanceof \WP_Term ) ) {
				return false;
			}

			if ( isset( $package_categories->enabled_group_discount[ $term->term_id ] ) && '1' == $package_categories->enabled_group_discount[ $term->term_id ] ) {
				return true;
			}
		}

		return false;
	}

	public static function instance( $trip_id ) {

		$trip_id = (int) $trip_id;

		if ( isset( self::$instance[ $trip_id ] ) ) {
			return self::$instance[ $trip_id ];
		}

		$trip = get_post( $trip_id );

		if ( ! ( $trip instanceof WP_Post ) ) {
			return false;
		}

		self::$instance[ $trip_id ] = new self( $trip );

		return self::$instance[ $trip_id ];
	}
}
