<?php

namespace Prodigy\Includes;

use Prodigy\Includes\Content\Prodigy_Request_Maker;
use Prodigy\Includes\Helpers\Prodigy_Parameters;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\includes\models\Prodigy_Order;

/**
 * Class Prodigy_Callback_Api_Listener
 */
class Prodigy_Callback_Api_Listener {

	private const CART_PAGE_NONCE_ACTION = 'cart-page-redirect-nonce';

	private const CONFIRMATION_STATE = 'confirmation';

	private const PAYMENT_STATE = 'payment';

	/** @var Prodigy_Cart */
	private Prodigy_Cart $cart;

	/** @var Prodigy_Order */
	private Prodigy_Order $order_model;

	/** @var Prodigy_Request_Maker */
	private Prodigy_Request_Maker $request_maker;

	/**
	 * @param   Prodigy_Order         $order_model
	 * @param   Prodigy_Request_Maker $request_maker
	 * @param   Prodigy_Cart          $cart
	 */
	public function __construct(
		Prodigy_Order $order_model,
		Prodigy_Request_Maker $request_maker,
		Prodigy_Cart $cart
	) {
		$this->order_model   = $order_model;
		$this->request_maker = $request_maker;
		$this->cart          = $cart;
	}

	/**
	 * @throws \Exception
	 */
	public function dispatch_url(): void {
		$order_token   = Prodigy_Parameters::get_parameter( $_GET, 'token' );
		$store_id      = Prodigy_Parameters::get_parameter( $_GET, 'redemption-store' );
		$login_session = Prodigy_Parameters::get_parameter( $_GET, 'session_token' );

		if ( ! $order_token ) {
			return;
		}

		if ( ! $login_session ) {
			$order = $this->order_model->get_in_process_order_for_token( $order_token );
			if ( ! empty( $order ) && ! Prodigy_User::is_logged_in() ) {
				$this->cart->set_cart_user_session( $order['local_user_hash'] );
			}
		}

		$order_data = $this->request_maker->do_order_data_request( $order_token );

		$redirect_url = $this->get_redirect_url(
			$order_token,
			$login_session,
			$store_id,
			$order_data
		);

		Prodigy_Url_Helper::redirect_to( $redirect_url );
	}

	/**
	 * @param   string      $order_token
	 * @param   string|null $login_session
	 * @param   string|null $store_id
	 * @param   array|null  $order_data
	 *
	 * @return string
	 */
	protected function get_redirect_url(
		string $order_token,
		?string $login_session,
		?string $store_id,
		?array $order_data = array()
	): string {
		$params = array( 'order_token' => $order_token );
		if ( ! empty( $login_session ) ) {
			$params['session_token'] = $login_session;
		}

		if ( ! empty( $order_data ) ) {
			$checkout_state = $order_data['data']['attributes']['checkout-state'] ?? '';
			if ( $checkout_state === self::CONFIRMATION_STATE ) {
				$this->order_model->update_status_orders( $order_token, Prodigy_Order::PRODIGY_ORDER_COMPLETED );
				if ( $store_id ) {
					$params['is_redemption_store'] = $store_id;
				}
			} elseif ( $checkout_state === self::PAYMENT_STATE && Prodigy_Parameters::get_parameter( $_GET, 'approval-needed' ) ) {
				$this->order_model->update_status_orders( $order_token, Prodigy_Order::PRODIGY_ORDER_COMPLETED );
				$params['approval-needed'] = true;
			} else {
				$params['_wpnonce'] = wp_create_nonce( self::CART_PAGE_NONCE_ACTION );

				return add_query_arg( $params, Prodigy_Url_Helper::prodigy_get_cart_url() );
			}
		}

		return add_query_arg( $params, Prodigy_Url_Helper::prodigy_get_thank_url() );
	}
}
