<?php

// this file is @generated
declare(strict_types=1);

namespace Svix\Api;

use Svix\Exception\ApiException;
use Svix\Models\CreateStreamEventsIn;
use Svix\Models\CreateStreamEventsOut;
use Svix\Models\EventStreamOut;
use Svix\Request\SvixHttpClient;

class StreamingEvents
{
    public function __construct(
        private readonly SvixHttpClient $client,
    ) {
    }

    /**
     * Iterate over a stream of events.
     *
     * The sink must be of type `poller` to use the poller endpoint.
     *
     * @throws ApiException
     */
    public function get(
        string $streamId,
        string $sinkId,
        StreamingEventsGetOptions $options = new StreamingEventsGetOptions(),
    ): EventStreamOut {
        $request = $this->client->newReq('GET', "/api/v1/stream/{$streamId}/sink/{$sinkId}/events");
        $request->setQueryParam('limit', $options->limit);
        $request->setQueryParam('iterator', $options->iterator);
        $request->setQueryParam('after', $options->after);
        $res = $this->client->send($request);

        return EventStreamOut::fromJson($res);
    }

    /**
     * Creates events on the Stream.
     *
     * @throws ApiException
     */
    public function create(
        string $streamId,
        CreateStreamEventsIn $createStreamEventsIn,
        StreamingEventsCreateOptions $options = new StreamingEventsCreateOptions(),
    ): CreateStreamEventsOut {
        $request = $this->client->newReq('POST', "/api/v1/stream/{$streamId}/events");
        $request->setHeaderParam('idempotency-key', $options->idempotencyKey);
        $request->setBody(json_encode($createStreamEventsIn));
        $res = $this->client->send($request);

        return CreateStreamEventsOut::fromJson($res);
    }
}
