<?php

declare(strict_types=1);

namespace App\Command;

use App\Entity\Customer;
use App\Entity\Options;
use App\Entity\Product;
use App\Service\Stripe\Client;
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: 'app:stripe:sync',
    description: 'Synchronise les clients locaux vers Stripe et configure les Webhooks'
)]
class StripeCommand extends Command
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
        private readonly Client $client,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        $io->title('Synchronisation Stripe : Clients & Webhooks');

        // 1. Synchronisation des clients manquants
        $io->section('Synchronisation des clients');
        $customers = $this->entityManager->getRepository(Customer::class)->findAll();

        foreach ($customers as $customer) {
            if($customer->getCustomerId() == null) {
                $result = $this->client->createCustomer($customer);

                if ($result['state']) {
                    $this->entityManager->persist($customer);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $customer->getEmail(), $result['message']));
                }
            } else {
                $result = $this->client->updateCustomer($customer);

                if ($result['state']) {
                    $this->entityManager->persist($customer);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $customer->getEmail(), $result['message']));
                }
            }
        }

        $io->section('Synchronisation des product');
        $products = $this->entityManager->getRepository(Product::class)->findAll();
        foreach ($products as $product) {
            if($product->getProductId() == null) {
                $result = $this->client->createProduct($product);
                if ($result['state']) {
                    $this->entityManager->persist($product);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $product->getName(), $result['message']));
                }
            } else {
                $result =  $this->client->updateProduct($product);
                if ($result['state']) {
                    $this->entityManager->persist($product);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $product->getName(), $result['message']));
                }
            }
        }

        $io->section('Synchronisation des options');
        $options = $this->entityManager->getRepository(Options::class)->findAll();
        foreach ($options as $option) {
            if($option->getStripeId() == "") {
                $result = $this->client->createOptions($option);
                if ($result['state']) {
                    $this->entityManager->persist($option);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $option->getName(), $result['message']));
                }
            } else {
                $result =  $this->client->updateOptions($option);
                if ($result['state']) {
                    $this->entityManager->persist($option);
                } else {
                    $io->error(sprintf('Échec pour %s : %s', $option->getName(), $result['message']));
                }
            }
        }
        // 2. Configuration des Webhooks
        $io->section('Configuration des Webhooks');
        $this->client->webhooks();
        $io->success('La configuration Stripe est à jour.');

        return Command::SUCCESS;
    }
}
