<?php

declare(strict_types=1);

namespace App\Scolarite\Infrastructure\Security;

use App\Administration\Domain\Model\User\Role;
use App\Administration\Infrastructure\Security\SecurityUser;

use function in_array;

use Override;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;

/**
 * Voter pour la consultation des devoirs par l'élève.
 *
 * @extends Voter<string, null>
 */
final class HomeworkStudentVoter extends Voter
{
    public const string VIEW = 'HOMEWORK_STUDENT_VIEW';

    #[Override]
    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === self::VIEW;
    }

    #[Override]
    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
    {
        $user = $token->getUser();

        if (!$user instanceof SecurityUser) {
            return false;
        }

        return in_array(Role::ELEVE->value, $user->getRoles(), true);
    }
}
