<?php

namespace App\Controller;
;
use Symfony\Component\HttpFoundation\Request;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class StripeController extends AbstractController
{
    #[Route('/stripe', name: 'app_stripe')]
    public function index(): Response
    {
        return $this->render('stripe/index.html.twig', [
            'controller_name' => 'StripeController',
        ]);
    }
    #[Route('/stripe/new',name:'app_stripe-new')]
    public function new(): JsonResponse
    {
        // Set your secret key. Remember to switch to your live secret key in production.
// See your keys here: https://dashboard.stripe.com/apikeys
        \Stripe\Stripe::setApiKey($this->getParameter('stripe_secret'));

        $pi = \Stripe\PaymentIntent::create([
            'amount' => 5000,
            'currency' => 'eur', // set currentcy to eur because giropay only supports eur
            'payment_method_types' => ['card'],
            'payment_method_options' => [
                'card' => [
                    'capture_method' => 'manual',
                ],
            ],
        ]);

        return new JsonResponse($pi,200);
    }
    #[Route('/stripe/update',name:'app_stripe-update', methods: ['POST'])]
    public function update(Request $request): JsonResponse
    {
        \Stripe\Stripe::setApiKey($this->getParameter('stripe_secret'));
        $intentId = $request->get('intent');
        $email = $request->get('email');
        $chargeId = $request->get('transaction');
        dump($intentId,$email,$chargeId);
        $stripe = new \Stripe\StripeClient($this->getParameter('stripe_secret'));
        $stripe->paymentIntents->update(
            $intentId,
            ['metadata' => [
                'email' => $email,
                'chargeId' => $chargeId
            ]]
        );

        $intent = \Stripe\PaymentIntent::retrieve($intentId);

        return new JsonResponse($intent,200);
    }
    #[Route('/stripe/test',name:'app_stripe-test', methods: ['POST'])]
    public function test(Request $request): JsonResponse
    {
        $chargeId = $request->get('transaction');
        $chargeId = 123456;

        $stripe = new \Stripe\StripeClient($this->getParameter('stripe_secret'));
        $res = $stripe->paymentIntents->search(['query' => 'metadata[\'chargeId\']:\''.$chargeId.'\'']);
        if(count($res->data)>0){
            $intent = $stripe->paymentIntents->retrieve($res->data[0]->id);
            $intent->capture(['amount_to_capture' => 750]);
        }
        return new JsonResponse($res->data[0]->id,200);
    }

}
