<?php

namespace App\Validator\Constraints;

use App\Dto\Input\PxeTemplateInput;
use App\Dto\Input\RemoteCalendarRuleInput;
use App\Entity\OgLive;
use App\Entity\PxeTemplate;
use App\Entity\RemoteCalendarRule;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class PxeTemplateUniqueDefaultValidator extends ConstraintValidator
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly RequestStack $requestStack
    )
    {
    }

    public function validate(mixed $value, Constraint $constraint): void
    {
        $request = $this->requestStack->getCurrentRequest();

        if (!$value instanceof PxeTemplateInput) {
            return;
        }

        if ($value->isDefault === false) {
            return;
        }

        $ogLiveDefault = $this->entityManager->getRepository(PxeTemplate::class)
            ->findOneBy([
                'isDefault' => true,
            ]);

        if ($ogLiveDefault) {
            $this->context->buildViolation($constraint->message)->addViolation();
        }

    }
}