<?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\MembersCotisations;
use App\Form\RequestPasswordConfirmType;
use App\Form\RequestPasswordRequestType;
use App\Repository\MembersCotisationsRepository;
use App\Service\Mailer\Mailer;
use App\Service\Payments\PaymentClient;
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\RedirectResponse;
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 DonsController extends AbstractController
{

    #[Route(path: '/cota/{id}/validation', name: 'app_cota_validate', options: ['sitemap' => false], methods: ['GET','POST'])]
    public function cotaValidation(?PaymentClient $paymentClient,Request $request,?MembersCotisations $membersCotisations): Response
    {
        if(!$membersCotisations instanceof MembersCotisations){
            return $this->redirectToRoute('app_login');
        }
        return $this->render('cota.twig',[
            'payment' => $membersCotisations
        ]);
    }

    #[Route(path: '/dons', name: 'app_dons', options: ['sitemap' => false], methods: ['GET','POST'])]
    public function index(Request $request,PaymentClient $paymentClient): Response
    {
        if($request->query->has('success')) {
            $dons = $request->getSession()->get('dons');

            $request->getSession()->remove('dons');
            return $this->render('dons_success.twig',[
                'dons' => $dons,
            ]);
        }
        if($request->isMethod('POST')) {

            $content = $request->request->all();

            $payment = $paymentClient->paymentDon($content);

            $content['id_payment'] = $payment->id;

            $request->getSession()->set('dons',$content);
            return new RedirectResponse($payment->url);
        }

        return $this->render('dons.twig',[
        ]);
    }
}
