<?php

namespace Eater\Glim\Handler\Panel;

use Aura\Session\Segment;
use Eater\Glim\Handler\Panel;
use Eater\Glim\Model\ServerQuery;

class Servers extends Panel
{
    public function handle()
    {
        /** @var Segment $segment */
        $segment = $this->get('session')->getSegment('main');
        $this->get('twig-vars')->def('message', $segment->getFlash('message'));
        
        $superuser = $this->getUser()->getSuperuser();
        $vars = [
            'superuser' => $superuser,
            'servers'   => $this->fetchServers()
        ];

        if ($superuser) {
            $vars['registered_servers'] = $this->fetchServers('registered');
        }

        return $this->render('panel/servers.html.twig', $vars);
    }

    public function fetchServers($status = "signed")
    {
        return ServerQuery::create()->filterByStatus($status)->find();
    }
}