<?php
/**
 * Free-plugin fallback stubs for Pro-only REST routes.
 *
 * When the Pro plugin is inactive, these endpoints return HTTP 402 with a
 * structured error so the React front-end can distinguish "Pro required"
 * from a genuine "endpoint missing" (404). When Pro IS active its real
 * controllers take precedence — these stubs must not register at all.
 *
 * Routes covered:
 *  GET|POST  /pro/stripe/settings
 *  POST      /pro/stripe/test
 *
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class ZINV_REST_Pro_Stubs {

	private string $namespace;

	public function __construct( string $namespace ) {
		$this->namespace = $namespace;
	}

	/**
	 * Register fallback routes — only when Pro plugin is inactive.
	 */
	public function register(): void {
		if ( apply_filters( 'zoventic_invoices_is_pro_active', false ) ) {
			return;
		}


		register_rest_route(
			$this->namespace,
			'/pro/stripe/test',
			[
				[
					'methods'             => WP_REST_Server::CREATABLE,
					'callback'            => [ $this, 'pro_required' ],
					'permission_callback' => [ $this, 'check_permission' ],
				],
			]
		);
	}

	/**
	 * Permission check — same capability gate as the real Pro endpoint.
	 */
	public function check_permission(): bool {
		return current_user_can( 'zinv_manage_settings' );
	}

	/**
	 * Returns a structured 402 so the UI can show an upgrade prompt rather
	 * than a generic network error.
	 */
	public function pro_required(): WP_Error {
		return new WP_Error(
			'zinv_pro_required',
			__( 'Stripe integration requires Zoventic Invoices Pro.', 'zoventic-invoices' ),
			[ 'status' => 402 ]
		);
	}
}
