<?php

declare(strict_types=1);

namespace App\Handler;

use App\Service\AwardeeManager;
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 AwardeeHandler implements RequestHandlerInterface
{
    /** @var TemplateRendererInterface */
    private $template;

    /** @var AwardeeManager */
    private $awardeeManager;

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

    public function handle(ServerRequestInterface $request) : ResponseInterface
    {
        $year = $request->getAttribute('year');
        $awardees = $this->awardeeManager->getJudgesByYear((int)$year);
        return new HtmlResponse($this->template->render("app::awardees", [
            'year' => $year,
            'awardees' => $awardees,
        ]));
    }
}
