<?php

namespace StevenBuehner\sbChurchtoolsGrouphomepage\Service;

use StevenBuehner\ChurchTools\Api\PersonApi;
use StevenBuehner\sbChurchtoolsGrouphomepage\Always\MyContainer;
use StevenBuehner\sbChurchtoolsGrouphomepage\Vendor\Symfony\Contracts\Cache\ItemInterface;

class CtPersonService {

	/**
	 * @var array|bool
	 */
	protected mixed $personCached        = FALSE;
	protected int   $personCachedSeconds = 60;

	public function __construct(protected PersonApi $personApi) {
	}

	public function getPerson(int $personId) {

		// ToDo hier muss noch was geändert werden - ich glaube nicht, dass dies Datenschutzukonform ist
		MyContainer::churchToolsLogin();

		return $this->personApi->getPersonById($personId)->getData();

		if ($this->personCached === FALSE) {
			$cacheAdapter = MyContainer::getCacheAdapter();

			$cacheAdapter->clear('churchtools_api_all_groups');


			$this->personCached = $cacheAdapter->get("churchtools_api_get_person_$personId", function (ItemInterface $item) use ($personId) {

				$item->expiresAfter($this->personCachedSeconds); // 60 Sek
				$item->expiresAfter(1);

				MyContainer::churchToolsLogin();

				return $this->personApi->getPersonById($personId)->getData();

			});

		}

		return $this->personCached;
	}

}
