<?php

namespace Opentalent\OtCore\Domain\Repository;

use GuzzleHttp\Client;
use Opentalent\OtCore\Exception\ApiRequestException;
use Opentalent\OtCore\Service\OpentalentApiService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 *  Base class for repositories based on the Opentalent API
 *
 */
abstract class BaseApiRepository
{
    protected const URI_TRAILING_PART = '';
    protected const HYDRA_TYPE = '';
    protected const DEFAULT_ITEMS_PER_PAGE = 8;

    protected $apiService;

    /**
     * BaseApiRepository constructor.
     */
    public function __construct($apiService = null) {
        $this->apiService = $apiService ?? GeneralUtility::makeInstance(OpentalentApiService::class);
    }

    /**
     * [FOR TESTS ONLY]
     * @param OpentalentApiService $apiService
     */
    protected function injectOpentalentApiService(OpentalentApiService $apiService): void
    {
        $this->apiService = $apiService;
    }

    /**
     * -- Needs to be reimplemented in subclasses --
     * Convert response's members record to an actual Domain's object
     * @param array $record
     * @return object
     */
    abstract protected function memberToObject(array $record): ?object;

    /**
     * Send a request to the API and
     * returns the first record (member)
     *
     * @param array $params
     * @param string|null $forceUri
     * @return object|null
     * @throws ApiRequestException
     */
    protected function getApiFirstRecord(array $params = [], ?string $forceUri = null): ?object
    {
        $params['page'] = '1';
        $params['totalItems'] = '1';
        $collection = $this->getApiRecords($params, $forceUri);
        return $collection->getMembers()[0] ?? null;
    }

    /**
     * Send a request to the API and
     * returns the records as an array (members)
     *
     * @param array $params
     * @param string|null $forceUri
     * @return ApiPagedCollection
     * @throws ApiRequestException
     */
    protected function getApiRecords(array $params = [], ?string $forceUri = null): ApiPagedCollection
    {
        $params = array("_format" => "json") + $params;   // _format should be the first param to match unit-tests fixtures
        if(!isset($params['itemsPerPage'])) {
            $params['itemsPerPage'] = (string)self::DEFAULT_ITEMS_PER_PAGE;
        }

        $uri = ltrim($this::URI_TRAILING_PART, '/');
        if ($forceUri !== null) {
            $uri = trim($forceUri, '/') . '/' . $uri;
        }
        $body = $this->apiService->getJsonDecoded($uri, $params);

        $page = (int)($params['page'] ?? 1);

        // build up the members
        $members = [];

        if (is_array($body['hydra:member'])) {
            foreach ($body['hydra:member'] as $record) {
                $instance = $this->memberToObject($record);
                if ($instance !== null) {
                    $members[] = $instance;
                }
            }
        }

        return new ApiPagedCollection(
            (int)($body['hydra:totalItems'] ?? 0),
            (int)($body['hydra:itemsPerPage'] ?? 0),
            $page,
            $members
        );
    }
}
