<?php

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

namespace Svix\Api;

use Svix\Exception\ApiException;
use Svix\Models\ConnectorIn;
use Svix\Models\ConnectorOut;
use Svix\Models\ConnectorPatch;
use Svix\Models\ConnectorUpsertIn;
use Svix\Models\ListResponseConnectorOut;
use Svix\Request\SvixHttpClient;

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

    /**
     * List all connectors for an application.
     *
     * @throws ApiException
     */
    public function list(
        ConnectorListOptions $options = new ConnectorListOptions(),
    ): ListResponseConnectorOut {
        $request = $this->client->newReq('GET', '/api/v1/connector');
        $request->setQueryParam('limit', $options->limit);
        $request->setQueryParam('iterator', $options->iterator);
        $request->setQueryParam('order', $options->order);
        $request->setQueryParam('product_type', $options->productType);
        $res = $this->client->send($request);

        return ListResponseConnectorOut::fromJson($res);
    }

    /**
     * Create a new connector.
     *
     * @throws ApiException
     */
    public function create(
        ConnectorIn $connectorIn,
        ConnectorCreateOptions $options = new ConnectorCreateOptions(),
    ): ConnectorOut {
        $request = $this->client->newReq('POST', '/api/v1/connector');
        $request->setHeaderParam('idempotency-key', $options->idempotencyKey);
        $request->setBody(json_encode($connectorIn));
        $res = $this->client->send($request);

        return ConnectorOut::fromJson($res);
    }

    /**
     * Get a connector.
     *
     * @throws ApiException
     */
    public function get(
        string $connectorId,
    ): ConnectorOut {
        $request = $this->client->newReq('GET', "/api/v1/connector/{$connectorId}");
        $res = $this->client->send($request);

        return ConnectorOut::fromJson($res);
    }

    /**
     * Create or update a connector.
     *
     * @throws ApiException
     */
    public function upsert(
        string $connectorId,
        ConnectorUpsertIn $connectorUpsertIn,
    ): ConnectorOut {
        $request = $this->client->newReq('PUT', "/api/v1/connector/{$connectorId}");
        $request->setBody(json_encode($connectorUpsertIn));
        $res = $this->client->send($request);

        return ConnectorOut::fromJson($res);
    }

    /**
     * Delete a connector.
     *
     * @throws ApiException
     */
    public function delete(
        string $connectorId,
    ): void {
        $request = $this->client->newReq('DELETE', "/api/v1/connector/{$connectorId}");
        $res = $this->client->sendNoResponseBody($request);
    }

    /**
     * Partially update a connector.
     *
     * @throws ApiException
     */
    public function patch(
        string $connectorId,
        ConnectorPatch $connectorPatch,
    ): ConnectorOut {
        $request = $this->client->newReq('PATCH', "/api/v1/connector/{$connectorId}");
        $request->setBody(json_encode($connectorPatch));
        $res = $this->client->send($request);

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