<?php

namespace App\Voter;

use App\Entity\User;
use App\Interface\OwnerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class OwnerApiEditVoter extends Voter
{
    public const EDIT = 'EDIT';

    public function __construct()
    {
    }

    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === self::EDIT && ($subject instanceof OwnerInterface);
    }

    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
    {
        $user = $token->getUser();
        // if the user is anonymous, do not grant access
        if (!$user instanceof User) {
            return false;
        }

        assert($subject instanceof OwnerInterface);

        // ... (check conditions and return true to grant permission) ...
        switch ($attribute) {
            case self::EDIT:
                if ($subject->getOwner()?->id === $user->getId()) {
                    //dd('sddds');
                    return true;
                }
                break;
        }
        return false;
    }
}
