<?php

declare( strict_types=1 );

namespace Brightfern\Support\Http;

/**
 * A simple HTTP response value object. `status` of 0 means the request failed
 * to complete (DNS/timeout/transport) — distinct from an HTTP error status, so
 * callers can tell "service unreachable" from "service said no".
 */
final class HttpResponse {
	public function __construct(
		public int $status,
		public string $body = '',
		public string $error = '',
	) {}

	public function ok(): bool {
		return $this->status >= 200 && $this->status < 300;
	}

	/** The transport itself failed (not an HTTP-level error). */
	public function transportFailed(): bool {
		return 0 === $this->status;
	}

	/** @return array<mixed>|null */
	public function json(): ?array {
		$decoded = json_decode( $this->body, true );
		return is_array( $decoded ) ? $decoded : null;
	}
}
