<?php

declare(strict_types=1);

namespace Brd6\NotionSdkPhp\Endpoint;

use Brd6\NotionSdkPhp\Exception\ApiResponseException;
use Brd6\NotionSdkPhp\Exception\HttpResponseException;
use Brd6\NotionSdkPhp\Exception\InvalidPaginationResponseException;
use Brd6\NotionSdkPhp\Exception\RequestTimeoutException;
use Brd6\NotionSdkPhp\Exception\UnsupportedPaginationResponseTypeException;
use Brd6\NotionSdkPhp\Exception\UnsupportedUserTypeException;
use Brd6\NotionSdkPhp\RequestParameters;
use Brd6\NotionSdkPhp\Resource\Pagination\AbstractPaginationResults;
use Brd6\NotionSdkPhp\Resource\Pagination\PaginationRequest;
use Brd6\NotionSdkPhp\Resource\User\AbstractUser;
use Http\Client\Exception;

class UsersEndpoint extends AbstractEndpoint
{
    /**
     * @param string $userId
     *
     * @throws ApiResponseException
     * @throws Exception
     * @throws HttpResponseException
     * @throws RequestTimeoutException
     * @throws UnsupportedUserTypeException
     */
    public function retrieve(string $userId): AbstractUser
    {
        $requestParameters = (new RequestParameters())
            ->setPath("users/$userId")
            ->setMethod('GET');

        $rawData = $this->getClient()->request($requestParameters);

        return AbstractUser::fromRawData($rawData);
    }

    /**
     * @param PaginationRequest|null $paginationRequest
     *
     * @throws ApiResponseException
     * @throws Exception
     * @throws HttpResponseException
     * @throws InvalidPaginationResponseException
     * @throws RequestTimeoutException
     * @throws UnsupportedPaginationResponseTypeException
     */
    public function list(?PaginationRequest $paginationRequest = null): AbstractPaginationResults
    {
        $paginationRequest = $paginationRequest ?? new PaginationRequest();

        $requestParameters = (new RequestParameters())
            ->setPath('users')
            ->setQuery($paginationRequest->toArray())
            ->setMethod('GET');

        $rawData = $this->getClient()->request($requestParameters);

        return AbstractPaginationResults::fromRawData($rawData);
    }

    /**
     * @throws ApiResponseException
     * @throws HttpResponseException
     * @throws RequestTimeoutException
     * @throws UnsupportedUserTypeException
     * @throws Exception
     */
    public function me(): AbstractUser
    {
        $requestParameters = (new RequestParameters())
            ->setPath('users/me')
            ->setMethod('GET');

        $rawData = $this->getClient()->request($requestParameters);

        return AbstractUser::fromRawData($rawData);
    }
}
