<?php

namespace App\Validator\Constraints;

use App\Dto\Input\OrganizationalUnitInput;
use App\Model\OrganizationalUnitP2PModes;
use App\Model\OrganizationalUnitTypes;
use App\Model\UserGroupPermissions;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class OrganizationalUnitParentValidator extends ConstraintValidator
{
    public function validate($value, Constraint $constraint): void
    {
        if (!$value instanceof OrganizationalUnitInput) {
            return;
        }

        if (!$value->parent && in_array($value->type, [OrganizationalUnitTypes::CLASSROOMS_GROUP, OrganizationalUnitTypes::CLASSROOM, OrganizationalUnitTypes::CLIENTS_GROUP])) {
            $this->context->buildViolation($constraint->message)->addViolation();
            return;
        }

        // Validar que el parent no sea la misma entidad que se está modificando
        if ($value->parent && $value->getOriginalEntity()) {
            if ($value->parent->getEntity()->getId() === $value->getOriginalEntity()->getId()) {
                $this->context->buildViolation($constraint->selfParentMessage)
                    ->atPath('parent')
                    ->addViolation();
            }
        }
    }
}