<?php

namespace Eater\Glim\Handler\Panel\Servers;

use Eater\Glim\Handler\Session;
use Eater\Glim\Model\ServerQuery;
use Eater\Glim\Service\CA;
use Eater\Glim\Service\Server;
use Symfony\Component\Config\Definition\Exception\Exception;

class Remove extends Session
{
    protected $shouldHaveSuperuser = true;

    public function handle()
    {
        $server = ServerQuery::create()->findOneByFingerprint($this->post('fingerprint'));
        if ($server === null || $server->getStatus() !== 'registered') {
            /** @var \Aura\Session\Session $session */
            $session = $this->get('session');
            $segment = $session->getSegment('main');

            $segment->setFlash("message", "Couldn't delete server that is already signed");

            return $this->redirect('/panel/servers');
        }

        $server->delete();

        return $this->redirect('/panel/servers');
    }
}