<?php

declare(strict_types=1);

namespace App\Handler;

use App\Entity\Awardee;
use Doctrine\ORM\EntityManager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Expressive\Template\TemplateRendererInterface;

class ProfileHandler implements RequestHandlerInterface
{
    /** @var TemplateRendererInterface */
    private $template;

    /** @var EntityManager */
    private $entityManager;

    public function __construct(
        TemplateRendererInterface $template,
        EntityManager $entityManager
    ) {
        $this->template = $template;
        $this->entityManager = $entityManager;
    }

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        $slug = $request->getAttribute('slug');
        $awardee = $this->entityManager->getRepository(Awardee::class)->findOneBy([
           'slug' => $slug,
        ]);
        return new HtmlResponse($this->template->render("app::profile", [
            'awardee' => $awardee,
        ]));
    }
}
