<?php
/**
 * affilun Cloud API exception.
 *
 * @package Affilun\Application\Cloud
 */

declare(strict_types=1);

namespace Affilun\Application\Cloud;

use RuntimeException;

/**
 * Carries sanitized Cloud error metadata for admin UI messages.
 */
final class CloudApiException extends RuntimeException {

	/**
	 * @param string $operation    Human-readable operation label.
	 * @param int    $statusCode   HTTP status code, or 0.
	 * @param string $cloudCode    Cloud error code.
	 * @param string $cloudMessage Cloud error message.
	 */
	public function __construct(
		string $operation,
		private int $statusCode = 0,
		private string $cloudCode = '',
		private string $cloudMessage = ''
	) {
		$parts = array( $operation . ' failed' );
		if ( $statusCode > 0 ) {
			$parts[] = 'HTTP ' . $statusCode;
		}
		if ( $cloudCode !== '' ) {
			$parts[] = $cloudCode;
		}

		parent::__construct( implode( ' / ', $parts ) . '.' );
	}

	/**
	 * HTTP status code returned by Cloud, or 0.
	 */
	public function statusCode(): int {
		return $this->statusCode;
	}

	/**
	 * Cloud error code.
	 */
	public function cloudCode(): string {
		return $this->cloudCode;
	}

	/**
	 * Cloud error message.
	 */
	public function cloudMessage(): string {
		return $this->cloudMessage;
	}
}
