<?php
/**
 * REST API controller for Cart Recovery Heartbeat.
 *
 * Handles periodic heartbeat requests to update cart timestamps,
 * preventing active users from being marked as abandoned.
 *
 * @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\REST\Security;
use EDD\Pro\CartRecovery\REST\Controllers\Heartbeat as Controller;

/**
 * Heartbeat controller class.
 *
 * @since 3.6.5
 */
final class Heartbeat extends Route {

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

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

	/**
	 * Register REST routes.
	 *
	 * @since 3.6.5
	 */
	public function register(): void {
		register_rest_route(
			self::NAMESPACE,
			'/' . self::$version . '/' . self::BASE . '/heartbeat',
			array(
				'methods'             => \WP_REST_Server::CREATABLE,
				'callback'            => array( $this->controller, 'handle_heartbeat' ),
				'permission_callback' => array( $this->security, 'validate_token' ),
			)
		);
	}
}
