<?php
/**
 * REST API routes for Cart Recovery Journey.
 *
 * Handles journey event tracking and funnel metrics for abandoned carts.
 *
 * @package EDD\Pro\CartRecovery\REST\Routes
 * @copyright Copyright (c) 2026, Sandhills Development, LLC
 * @license https://opensource.org/licenses/GPL-3.0 GNU General Public License v3.0
 * @since 3.6.5
 */

namespace EDD\Pro\CartRecovery\REST\Routes;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\REST\Routes\Route;
use EDD\Pro\CartRecovery\REST\Controllers\Journey as Controller;

/**
 * Journey route class.
 *
 * @since 3.6.5
 */
final class Journey extends Route {

	/**
	 * REST API base.
	 *
	 * @since 3.6.5
	 * @var string
	 */
	const BASE = 'acr/journey';

	/**
	 * Constructor.
	 *
	 * @since 3.6.5
	 */
	public function __construct() {
		$this->controller = new Controller();
	}

	/**
	 * Register REST routes.
	 *
	 * @since 3.6.5
	 */
	public function register(): void {
		// Get journey events for a specific cart.
		register_rest_route(
			self::NAMESPACE,
			'/' . self::$version . '/' . self::BASE . '/(?P<cart_id>[\d]+)',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this->controller, 'get_cart_journey' ),
				'permission_callback' => array( $this->controller, 'get_items_permissions_check' ),
				'args'                => array(
					'cart_id' => array(
						'description' => __( 'Unique identifier for the cart.', 'easy-digital-downloads' ),
						'type'        => 'integer',
						'required'    => true,
					),
				),
			)
		);

		// Get funnel metrics.
		register_rest_route(
			self::NAMESPACE,
			'/' . self::$version . '/' . self::BASE . '/funnel',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this->controller, 'get_funnel_metrics' ),
				'permission_callback' => array( $this->controller, 'get_items_permissions_check' ),
				'args'                => array(
					'start_date' => array(
						'description' => __( 'Start date (Y-m-d format).', 'easy-digital-downloads' ),
						'type'        => 'string',
						'format'      => 'date',
					),
					'end_date'   => array(
						'description' => __( 'End date (Y-m-d format).', 'easy-digital-downloads' ),
						'type'        => 'string',
						'format'      => 'date',
					),
				),
			)
		);

		// Get average time between events.
		register_rest_route(
			self::NAMESPACE,
			'/' . self::$version . '/' . self::BASE . '/timing',
			array(
				'methods'             => \WP_REST_Server::READABLE,
				'callback'            => array( $this->controller, 'get_timing_metrics' ),
				'permission_callback' => array( $this->controller, 'get_items_permissions_check' ),
				'args'                => array(
					'from_event' => array(
						'description' => __( 'Starting event type.', 'easy-digital-downloads' ),
						'type'        => 'string',
						'required'    => true,
					),
					'to_event'   => array(
						'description' => __( 'Ending event type.', 'easy-digital-downloads' ),
						'type'        => 'string',
						'required'    => true,
					),
					'start_date' => array(
						'description' => __( 'Start date (Y-m-d format).', 'easy-digital-downloads' ),
						'type'        => 'string',
						'format'      => 'date',
					),
					'end_date'   => array(
						'description' => __( 'End date (Y-m-d format).', 'easy-digital-downloads' ),
						'type'        => 'string',
						'format'      => 'date',
					),
				),
			)
		);
	}
}
