<?php
defined( 'ABSPATH' ) || exit;

/**
 * Abstract base for all TukiFlow REST endpoints.
 */
abstract class TFDC_Endpoint_Base extends WP_REST_Controller {

	/**
	 * REST namespace.
	 *
	 * @var string
	 */
	protected $namespace = 'tukiflow/v1';

	/**
	 * Permission callback: verify the WP REST nonce.
	 * Used for POST endpoints that mutate state.
	 *
	 * @param WP_REST_Request $request
	 * @return true|WP_Error
	 */
	public function check_nonce( WP_REST_Request $request ) {
		$nonce = $request->get_header( 'X-WP-Nonce' );
		if ( ! $nonce || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
			return new WP_Error(
				'rest_forbidden',
				__( 'Invalid nonce.', 'tukiflow-direct-checkout-woocommerce' ),
				array( 'status' => 403 )
			);
		}
		return true;
	}

	/**
	 * Return a WP_Error with proper status code.
	 *
	 * @param string $message Error message.
	 * @param int    $status  HTTP status code.
	 * @return WP_Error
	 */
	protected function json_error( $message, $status = 400 ) {
		return new WP_Error( 'tfdc_error', $message, array( 'status' => $status ) );
	}
}
