<?php

declare(strict_types=1);

namespace App\Administration\Infrastructure\Security;

use App\Administration\Domain\Model\TeacherAssignment\TeacherAssignment;
use App\Administration\Domain\Model\User\Role;
use App\Administration\Infrastructure\Api\Resource\TeacherAssignmentResource;

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 les autorisations sur les affectations enseignants.
 *
 * Règles d'accès :
 * - ADMIN et SUPER_ADMIN : accès complet (CRUD)
 * - ENSEIGNANT : lecture seule (ses propres affectations)
 * - VIE_SCOLAIRE, SECRETARIAT : lecture seule
 * - ELEVE et PARENT : pas d'accès direct
 *
 * @extends Voter<string, TeacherAssignment|TeacherAssignmentResource>
 */
final class TeacherAssignmentVoter extends Voter
{
    public const string VIEW = 'TEACHER_ASSIGNMENT_VIEW';
    public const string CREATE = 'TEACHER_ASSIGNMENT_CREATE';
    public const string DELETE = 'TEACHER_ASSIGNMENT_DELETE';

    private const array SUPPORTED_ATTRIBUTES = [
        self::VIEW,
        self::CREATE,
        self::DELETE,
    ];

    #[Override]
    protected function supports(string $attribute, mixed $subject): bool
    {
        if (!in_array($attribute, self::SUPPORTED_ATTRIBUTES, true)) {
            return false;
        }

        if ($subject === null) {
            return true;
        }

        return $subject instanceof TeacherAssignment || $subject instanceof TeacherAssignmentResource;
    }

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

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

        $roles = $user->getRoles();

        return match ($attribute) {
            self::VIEW => $this->canView($roles, $user, $subject),
            self::CREATE => $this->canCreate($roles),
            self::DELETE => $this->canDelete($roles),
            default => false,
        };
    }

    /**
     * @param string[] $roles
     */
    private function canView(array $roles, SecurityUser $user, mixed $subject): bool
    {
        // Admins et personnel administratif : accès complet en lecture
        if ($this->hasAnyRole($roles, [
            Role::SUPER_ADMIN->value,
            Role::ADMIN->value,
            Role::VIE_SCOLAIRE->value,
            Role::SECRETARIAT->value,
        ])) {
            return true;
        }

        // Enseignant : lecture seule de ses propres affectations
        if ($this->hasAnyRole($roles, [Role::PROF->value])) {
            return $this->isOwnResource($user, $subject);
        }

        return false;
    }

    /**
     * Vérifie que la ressource appartient à l'enseignant connecté.
     */
    private function isOwnResource(SecurityUser $user, mixed $subject): bool
    {
        if ($subject instanceof TeacherAssignment) {
            return (string) $subject->teacherId === $user->userId();
        }

        if ($subject instanceof TeacherAssignmentResource) {
            return $subject->teacherId === $user->userId();
        }

        // Pas de sujet (collection sans filtre) : refuser par défaut
        return false;
    }

    /**
     * @param string[] $roles
     */
    private function canCreate(array $roles): bool
    {
        return $this->hasAnyRole($roles, [
            Role::SUPER_ADMIN->value,
            Role::ADMIN->value,
        ]);
    }

    /**
     * @param string[] $roles
     */
    private function canDelete(array $roles): bool
    {
        return $this->hasAnyRole($roles, [
            Role::SUPER_ADMIN->value,
            Role::ADMIN->value,
        ]);
    }

    /**
     * @param string[] $userRoles
     * @param string[] $allowedRoles
     */
    private function hasAnyRole(array $userRoles, array $allowedRoles): bool
    {
        foreach ($userRoles as $role) {
            if (in_array($role, $allowedRoles, true)) {
                return true;
            }
        }

        return false;
    }
}
