<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Components\Event\Event;
use App\Model\Services\EventService;

class EventReader extends BaseReader
{
    /** @var EventService */
    protected $eventService;
    /** @var SiteReader */
    protected $siteReader;
    /** @var UserReader */
    protected $userReader;

    public function __construct(EventService $eventService, SiteReader $siteReader, UserReader $userReader)
    {
        $this->eventService = $eventService;
        $this->siteReader = $siteReader;
        $this->userReader = $userReader;
    }

    public function findEventById(int $id) : Event
    {
        $event = $this->eventService->findEventById($id);
        $this->validateEntity($event);

        return $event;
    }

    /**
     * @param $site
     * @return Event[]
     */
    public function findUpcomingEvents($site) : array
    {
        $siteEntity = $this->siteReader->validateSite($site);
        return $this->eventService->findUpcomingEvents($siteEntity);
    }

    public function validateEvent($event) : Event
    {
        if ($event instanceof Event) {
            return $event;
        }

        return $this->findEventById($event);
    }

    public function findUserEvents($user)
    {
        $userEntity = $this->userReader->validateUser($user);
        return $this->eventService->findUserEvents($userEntity);
    }

}