<?php

namespace App\Controller;

use App\Entity\Facture;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class FactureVerifyController extends AbstractController
{
    #[Route('/facture/verify/{id}/{hmac}', name: 'app_facture_verify', requirements: ['id' => '\d+'])]
    public function index(int $id, string $hmac, EntityManagerInterface $em): Response
    {
        $facture = $em->getRepository(Facture::class)->find($id);

        if (null === $facture || !hash_equals($facture->getHmac(), $hmac)) {
            return $this->render('facture/verify_invalid.html.twig');
        }

        return $this->render('facture/verify.html.twig', [
            'facture' => $facture,
            'customer' => $facture->getCustomer(),
        ]);
    }
}
