<?php

namespace Eater\Glim\Handler\Panel\Invites;


use Eater\Glim\Handler\Session;
use Eater\Glim\Service\User;
use Aura\Session\Segment;

class Create extends Session
{
    public function handle()
    {
        /** @var User $user */
        $user = $this->get('user');

        $userModel = $this->getUser();
        
        if ($userModel->getUsedInvites() < $userModel->getMaxInvites() || $userModel->getMaxInvites() === -1) {
            $user->createInvite($userModel);
        } else {
            /** @var Segment $segment */
            $segment = $this->get('session')->getSegment('main');
            $segment->setFlash('error', 'You dont have any invites anymore');
        }

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