<?php
/**
 * REST API Route for Cart Recovery Customer Info.
 *
 * Handles customer information capture during checkout (email, first name, last name).
 *
 * @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;

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

/**
 * CustomerInfo route class.
 *
 * @since 3.6.5
 */
final class CustomerInfo 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 . '/customer-info',
			array(
				'methods'             => \WP_REST_Server::CREATABLE,
				'callback'            => array( $this->controller, 'handle_customer_info' ),
				'permission_callback' => array( $this->security, 'validate_token' ),
				'args'                => array(
					'email'      => array(
						'required'          => false,
						'type'              => 'string',
						'sanitize_callback' => 'sanitize_email',
						'validate_callback' => function ( $param ) {
							return empty( $param ) || is_email( $param );
						},
					),
					'first_name' => array(
						'required'          => false,
						'type'              => 'string',
						'sanitize_callback' => 'sanitize_text_field',
					),
					'last_name'  => array(
						'required'          => false,
						'type'              => 'string',
						'sanitize_callback' => 'sanitize_text_field',
					),
				),
			)
		);
	}
}
