<?php declare(strict_types=1);
/**
 * @package messenger
 * @author kputyr
 * @date 20.04.2026
 */

namespace IMATHUZH\Messenger\Nchan;

/**
 * A wrapped around an HTTP response from Nchan to publishing
 * a message on an nchan location that exposes the given
 * properties of the channel.
 */
class NchanResponse
{
    public function __construct(
        public array $headers,
        public string $body
    ) { }

    public function asJson(): ?object {
        static $parsedBody;
        if (!isset($parsedBody)) {
            $parsedBody = json_decode($this->body, false);
        }
        return $parsedBody;
    }

    public function getLastMessageId(): string {
        return $this->asJson()->last_message_id ?? '';
    }

    public function getSubscribers(): int {
        return $this->asJson()->subscribers ?? 0;
    }

    public function getMessageCount(): int {
        return $this->asJson()->messages ?? 0;
    }

    public function getLastTimeRequested(): int {
        return $this->asJson()->requested ?? 0;
    }
}
