<?php

namespace App\Command;

use App\Entity\Account;
use App\Entity\MembersCotisations;
use App\Repository\MembersCotisationsRepository;
use App\Repository\MembersRepository;
use App\Service\Generator\TempPasswordGenerator;
use App\Service\Mailer\Event\CreatedAdminEvent;
use App\Service\Mailer\Mailer;
use App\Service\Payments\PaymentClient;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Uid\Uuid;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[AsCommand(name: 'ecosplay:cota')]
class CotaCommand extends Command
{
    public function __construct(
        private readonly MembersRepository $membersRepository,
        private readonly MembersCotisationsRepository $membersCotisationsRepository,
        private readonly EntityManagerInterface $entityManager,
        private readonly Mailer $mailer,
        private readonly PaymentClient $paymentClient,
        ?string $name = null
    ) {
        parent::__construct($name);
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title("Création cota");

        foreach ($this->membersRepository->findAll() as $member) {
            if($member->getRole() =="Membre") {
                $t = new \DateTimeImmutable();
                $tNext = new \DateTimeImmutable();
                $tNext = $tNext->modify("+1 year");

                $cota = $member->getMembersCotisations()->filter(function (MembersCotisations $membersCotisations) use ($t, $tNext) {
                    return ($membersCotisations->getStartdATE()->format('Y') == $t->format('Y')) && ($membersCotisations->getEndDate()->format('Y') == $tNext->format('Y'));
                })->first();
                if (!$cota instanceof MembersCotisations) {
                    $cota = new MembersCotisations();
                    $cota->setIsPaid(false);
                    $cota->setAmount(15);
                    $cota->setStartdATE($t);
                    $cota->setEndDate($tNext);
                    $cota->setMembers($member);
                    $this->entityManager->persist($cota);
                    $this->entityManager->flush();
                    $link = $this->paymentClient->paymentCota($cota);
                    $cota->setPaymentId($link->id);
                    $paymentLink = $link->url;
                    $this->mailer->send($member->getEmail(), $member->getPseudo(), "[E-Cosplay] - Lien de paiement de votre cotisation", "mails/coti_payment.twig", [
                        'pseudo' => $member->getPseudo(),
                        'link' => $paymentLink,
                        'amount' => $cota->getAmount(),
                        'start_at' => $cota->getStartdATE(),
                        'end_at' => $cota->getEndDate(),
                    ]);
                    $this->entityManager->persist($cota);
                    $this->entityManager->flush();
                }
            }
        }

        $this->entityManager->flush();
        return Command::SUCCESS;
    }
}
