<?php
/**
 * 2018-2019 Alma SAS
 *
 * THE MIT LICENSE
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR Alma_Gateway_A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @author    Alma SAS <contact@getalma.eu>
 * @copyright 2018-2019 Alma SAS
 * @license   https://opensource.org/licenses/MIT The MIT License
 */


namespace Alma\Vendor\Alma\Client\Application\Endpoint;

use Alma\Vendor\Alma\Client\Application\Exception\Endpoint\WebhookEndpointException;
use Alma\Vendor\Alma\Client\Application\Helper\WebhookHelper;
use Alma\Vendor\Psr\Http\Client\ClientExceptionInterface;

class WebhookEndpoint extends AbstractEndpoint
{
    const WEBHOOKS_ENDPOINT = '/v1/webhooks';

    /**
     * @param string $type The webhook type, see available constants in Alma\Client\Entities\Webhook
     * @param string $url The URL to be called for that webhook
     *
     * @return WebhookHelper
     * @throws WebhookEndpointException
     */
    public function create(string $type, string $url): WebhookHelper
    {
        $data = array("type" => $type, "url" => $url);

        try {
            $request = null;
            $request = $this->createPostRequest(self::WEBHOOKS_ENDPOINT, $data);
            $response = $this->client->sendRequest($request);
        } catch (ClientExceptionInterface $e) {
            throw new WebhookEndpointException($e->getMessage(), $request);
        }

        if ($response->isError()) {
            throw new WebhookEndpointException($response->getReasonPhrase(), $request, $response);
        }

        return new WebhookHelper($response->getJson());
    }

    /**
     * @param string $id The external ID for the payment to fetch
     *
     * @return WebhookHelper
     * @throws WebhookEndpointException
     */
    public function fetch(string $id): WebhookHelper
    {
        try {
            $request = null;
            $request = $this->createGetRequest(self::WEBHOOKS_ENDPOINT . sprintf("/%s", $id));
            $response = $this->client->sendRequest($request);
        } catch (ClientExceptionInterface $e) {
            throw new WebhookEndpointException($e->getMessage(), $request);
        }

        if ($response->isError()) {
            throw new WebhookEndpointException($response->getReasonPhrase(), $request, $response);
        }

        return new WebhookHelper($response->getJson());
    }

    /**
     * Delete a webhook
     *
     * @param string $id The ID of the Webhook to delete
     *
     * @return true True if the deletion call worked (throws otherwise)
     * @throws WebhookEndpointException
     */
    public function delete(string $id): bool
    {
        try {
            $request = null;
            $request = $this->createDeleteRequest(self::WEBHOOKS_ENDPOINT . sprintf("/%s", $id));
            $response = $this->client->sendRequest($request);
        } catch (ClientExceptionInterface $e) {
            throw new WebhookEndpointException($e->getMessage(), $request);
        }
        if ($response->isError()) {
            throw new WebhookEndpointException($response->getReasonPhrase(), $request, $response);
        }

        return true;
    }
}
