<?php

declare(strict_types=1);

namespace App\Security\Voter\EntityVoter\Core;

use App\Entity\Core\BankAccount;
use App\Entity\Core\ContactPoint;
use App\Security\Voter\EntityVoter\AbstractEntityVoter;

class ContactPointVoter extends AbstractEntityVoter
{
    protected static ?string $entityClass = ContactPoint::class;

    protected static array $allowedOperations = [
        self::READ, self::EDIT, self::DELETE,
    ];

    /**
     * Can the user interact with the ContactPoint with the given required role.
     *
     * @param string $role The role needed to interract with the ContactPoint
     */
    private function canInteractIfHasRole(object $subject, string $role): bool
    {
        return $this->isUserLoggedIn()
            && $subject->getOrganization()->count() === 1
            && $this->security->isGranted($role)
            && $subject->getOrganization()->current()->getId() === $this->getUser()->getOrganization()->getId();
    }

    /**
     * @param $subject BankAccount
     */
    protected function canView(object $subject): bool
    {
        return $this->canInteractIfHasRole($subject, 'ROLE_ORGANIZATION_VIEW');
    }

    /**
     * @param $subject BankAccount
     */
    protected function canEdit(object $subject): bool
    {
        return $this->canInteractIfHasRole($subject, 'ROLE_ORGANIZATION');
    }

    /**
     * @param $subject BankAccount
     */
    protected function canDelete(object $subject): bool
    {
        return $this->canEdit($subject);
    }
}
