<?php

namespace App\Command;

use App\Entity\Abonements;
use App\Service\Payments\PaymentClient;
use Doctrine\ORM\EntityManager;
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\Style\SymfonyStyle;

#[AsCommand(name: 'ecosplay:abonement')]
class PayAbonementCommand extends Command
{

    public function __construct(protected readonly EntityManagerInterface $entityManager,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("EPage - Abonement");
        $abos =[
            ['name'=>'Epage 1 Mois','price'=>5,'time'=>1],
            ['name'=>'Epage 2 Mois','price'=>8,'time'=>2],
            ['name'=>'Epage 3 Mois','price'=>12,'time'=>3],
            ['name'=>'Epage 6 Mois','price'=>25,'time'=>6],
            ['name'=>'Epage 12 Mois','price'=>40,'time'=>12],
        ];
        foreach ($abos as $abos) {
            $abo = $this->entityManager->getRepository(Abonements::class)->findOneBy(['name'=>$abos['name']]);
            if(!$abo instanceof Abonements){
                $abo = new Abonements();
                $abo->setName($abos['name']);
                $abo->setCreateAt(new \DateTimeImmutable());
                $abo->setIsActif(true);
                $abo->setTva(1);
                $abo->setDuration($abos['time']);
            }
            $abo->setPriceHt($abos['price']);
            $abo->setPriceTtc($abos['price'] *$abo->getTva() );
            $abo->setUpdateAt(new \DateTimeImmutable());
            if($abo->getStripeId() == null){
                $stripeProduct = $this->paymentClient->createAbos($abo);
                $abo->setStripeId($stripeProduct);
            }
            $this->entityManager->persist($abo);
            $this->entityManager->flush();
        }

        return Command::SUCCESS;
    }
}
