<?php

namespace App\Controller\Trait;

use App\Entity\Event;
use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;

trait AccountEventOwnershipTrait
{
    private function requireEventOwnership(Event $event): User
    {
        /** @var User $user */
        $user = $this->getUser();
        if ($this->isGranted('ROLE_ROOT')) {
            return $event->getAccount();
        }
        if ($event->getAccount()->getId() !== $user->getId()) {
            throw $this->createAccessDeniedException();
        }

        return $user;
    }

    /** @codeCoverageIgnore Tested via testOrganizerWithoutStripeBlocksEventCreation */
    private function requireStripeReady(): ?Response
    {
        if ($this->isGranted('ROLE_ROOT')) {
            return null;
        }

        /** @var User $user */
        $user = $this->getUser();

        $stripeNotReady = !$user->isStripeChargesEnabled() || !$user->isStripePayoutsEnabled();
        $billingBlocked = $user->isBilling() && 'good' !== $user->getBillingState();

        return ($stripeNotReady || $billingBlocked) ? $this->redirectToRoute('app_account') : null;
    }
}
