<?php

namespace App\Service\Payments;

use App\Entity\MembersCotisations;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class PaymentClient
{

    private string $url;

    public function __construct(private readonly RequestStack $requestStack, private readonly UrlGeneratorInterface $urlGenerator, private readonly EntityManagerInterface $em)
    {
        if($_ENV['APP_ENV'] == "prod")
            $this->url = $this->requestStack->getCurrentRequest()->getSchemeAndHttpHost();
        else
            $this->url = $_ENV['DEV_URL'];
    }

    public function paymentDon(array $content)
    {
        $stripe = new \Stripe\StripeClient($_ENV['STRIPE_SK']);

        $checkoutSession = $stripe->checkout->sessions->create([
            'customer_email' => $content['email'],
            'line_items' => [[
                'price_data' => [
                    'currency' => 'eur',
                    'product_data' => [
                        'name' => "Dons ".$content['amount']." €",
                        'description' => "Dons ".$content['amount']." €",
                    ],
                    'unit_amount' => $content['amount']*100,
                ],
                'quantity' => 1,
            ]],
            'metadata' => $content,
            'mode' => 'payment',
            'success_url' => $this->url.$this->urlGenerator->generate('app_dons',['success' => true]),
        ]);

        return $checkoutSession;
    }

    public function validateWebhooks($payload)
    {
        try {
            $event = \Stripe\Event::constructFrom(
                json_decode($payload, true)
            );
            return true;
        } catch(\UnexpectedValueException $e) {
            return false;
        }
    }

    public function paymentCota(MembersCotisations $membersCotisations) {
        $stripe = new \Stripe\StripeClient($_ENV['STRIPE_SK']);
        $checkoutSession = $stripe->checkout->sessions->create([
            'customer_email' => $membersCotisations->getMembers()->getEmail(),
            'line_items' => [[
                'price_data' => [
                    'currency' => 'eur',
                    'product_data' => [
                        'name' => "Cotisations",
                        'description' => "Cotisations",
                    ],
                    'unit_amount' => $membersCotisations->getAmount()*100,
                ],
                'quantity' => 1,
            ]],
            'metadata' => [
                'id_con' => $membersCotisations->getId(),
            ],
            'mode' => 'payment',
            'success_url' => $this->url.$this->urlGenerator->generate('app_cota_validate',['id'=>$membersCotisations->getId()]),
        ]);

        return $checkoutSession;
    }

    public function createAbos(\App\Entity\Abonements $abo)
    {
        $stripe = new \Stripe\StripeClient($_ENV['STRIPE_SK']);
        $product = $stripe->products->create([
            'name' => $abo->getName(),
            'active' => true,
            'description' => $abo->getName(),
            'default_price_data' => [
                'currency' => 'EUR',
                'recurring' => [
                    'interval' => 'month',
                    'interval_count' => $abo->getDuration(),
                ],
                'tax_behavior' => 'unspecified',
                'unit_amount' => $abo->getPriceTtc()*100,

            ],
        ]);
        return $product->id;

    }
}
