<?php

namespace App\Controller;

use App\Dto\Contact\ContactType;
use App\Dto\Contact\DtoContact;
use App\Entity\Account;
use App\Entity\AccountResetPasswordRequest;
use App\Entity\Ag\Main;
use App\Entity\Ag\MainMember;
use App\Entity\MembersCotisations;
use App\Form\RequestPasswordConfirmType;
use App\Form\RequestPasswordRequestType;
use App\Repository\Ag\MainMemberRepository;
use App\Repository\Ag\MainRepository;
use App\Repository\Ag\MainSignedRepository;
use App\Service\Mailer\Mailer;
use App\Service\ResetPassword\Event\ResetPasswordConfirmEvent;
use App\Service\ResetPassword\Event\ResetPasswordEvent;
use Doctrine\ORM\EntityManagerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Twig\Environment;

class AdController extends AbstractController
{

    #[Route(path: '/adh/{id}', name: 'app_adh', options: ['sitemap' => false], methods: ['GET','POST'])]
    public function adh(?Main $main,Request $request,EntityManagerInterface $entityManager,MainMemberRepository $mainMemberRepository,MainSignedRepository $mainSignedRepository): Response
    {
        if(!$main instanceof Main){
            return $this->redirectToRoute('app_home');
        }

        if($request->query->has('validateResult')) {

            $vr = $mainSignedRepository->find($request->query->get('vr'));
            $r = $mainMemberRepository->findOneBy(['main'=>$main,'member'=>$vr->getMembers()]);
            if(!$r instanceof MainMember){
                $r = new MainMember();
                $r->setMain($main);
                $r->setMember($vr->getMembers());
                $t = new \DateTime();
                $tz = new \DateTime();
                $tz->modify('+1 year');
                $isValid = $vr->getMembers()->getMembersCotisations()->filter(function (MembersCotisations $membersCotisations) use ($t,$tz) {
                    return ($membersCotisations->getStartdATE()->format('Y') == $t->format('Y')) && ($membersCotisations->getEndDate()->format('Y') == $tz->format('Y'));
                })->first();

                $r->setIsVotedAllow($isValid instanceof MembersCotisations);
                $entityManager->persist($r);
                $entityManager->flush();
            }
            return $this->render('adh_validate.twig',[
                'main' => $main,
            ]);
        }
        $signedList =[];
        foreach ($main->getMainSigneds() as $signed) {
            $docuseal = new \Docuseal\Api('pgAU116mCFmeF7WQSezHqxtZW8V1fgo31u5d2FXoaKe', 'https://signature.esy-web.dev/api');
            $submiter = $docuseal->getSubmitter($signed->getSubmiterId());
            $signed->sign = "https://signature.esy-web.dev/s/".$submiter['slug'];
            $signedList[] = $signed;
        }
        return $this->render('adh.twig',[
            'main' => $main,
            'signed' => $signedList,
        ]);
    }
}
