<?php
/**
 * API helpers.
 *
 * @package photoshelter-importer
 */

namespace PhotoShelter\Importer\Helper\API;

/**
 * Error response formatter.
 *
 * @param \WP_Error $error WordPress error object.
 * @param integer   $status Error status code.
 *
 * @return \WP_REST_Response|\WP_Error
 * @since 1.0.0
 */
function error_response( $error, $status ) {
	if ( true === is_wp_error( $error ) ) {
		// Log error details when WP_DEBUG is enabled.
		log_error_response( $error, $status );

		$error_code    = $error->get_error_code();
		$error_message = $error->get_error_message();

		// Determine if this is a permission-related error.
		$is_permission_error = is_permission_error( $error_code, $error_message );

		$response_data = array(
			'code'    => $error_code,
			'message' => $error_message,
			'data'    => array(
				'status' => $status,
			),
		);

		// Add helpful context for permission errors.
		if ( true === $is_permission_error ) {
			$response_data['data']['error_type'] = 'permission';
			$response_data['data']['help']       = __( 'This error typically occurs when your PhotoShelter account does not have permission to download this file format. Try selecting a different download quality, or contact your PhotoShelter administrator.', 'photoshelter-importer' );
		}

		return new \WP_REST_Response( $response_data, $status );
	}

	return $error;
}

/**
 * Check if an error is permission-related.
 *
 * @since 1.3.0
 *
 * @param string $error_code    The error code.
 * @param string $error_message The error message.
 * @return bool True if this is a permission error.
 */
function is_permission_error( $error_code, $error_message ) {
	$permission_indicators = array(
		'permission',
		'AcsPermission',
		'unauthorized',
		'not authorized',
		'access denied',
		'forbidden',
	);

	$check_string = strtolower( $error_code . ' ' . $error_message );

	foreach ( $permission_indicators as $indicator ) {
		if ( false !== strpos( $check_string, strtolower( $indicator ) ) ) {
			return true;
		}
	}

	return false;
}

/**
 * Log error response details for debugging.
 *
 * @since 1.3.0
 *
 * @param \WP_Error $error  WordPress error object.
 * @param int       $status HTTP status code.
 * @return void
 */
function log_error_response( $error, $status ) {
	// Only log if WP_DEBUG is enabled.
	if ( false === defined( 'WP_DEBUG' ) || true !== WP_DEBUG ) {
		return;
	}

	$error_data = $error->get_error_data();

	// phpcs:disable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
	error_log( '=== PhotoShelter Error Response Debug ===' );
	error_log( 'Error Code: ' . $error->get_error_code() );
	error_log( 'Error Message: ' . $error->get_error_message() );
	error_log( 'HTTP Status: ' . $status );
	error_log( 'Error Data: ' . print_r( $error_data, true ) );
	error_log( '=== End PhotoShelter Error Response Debug ===' );
	// phpcs:enable WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
}
