<?php

namespace App\Controller;

use App\Repository\AttestationRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Attribute\Route;

class AttestationController extends AbstractController
{
    #[Route('/attestation/{reference}', name: 'app_attestation_verify', methods: ['GET'])]
    public function verify(
        string $reference,
        AttestationRepository $repository,
        #[Autowire('%env(APP_SECRET)%')] string $hmacSecret,
    ): Response {
        $attestation = $repository->findOneBy(['reference' => $reference]);

        if (null === $attestation) {
            return $this->render('attestation/not_found.html.twig', [
                'reference' => $reference,
            ]);
        }

        $valid = $attestation->verify($hmacSecret);

        return $this->render('attestation/verify.html.twig', [
            'attestation' => $attestation,
            'valid' => $valid,
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Verification attestation'],
            ],
        ]);
    }

    #[Route('/attestation/{reference}/download', name: 'app_attestation_download', methods: ['GET'])]
    public function download(
        string $reference,
        AttestationRepository $repository,
    ): Response {
        $attestation = $repository->findOneBy(['reference' => $reference]);

        if (null === $attestation) {
            throw $this->createNotFoundException('Attestation introuvable.');
        }

        $path = $attestation->getPdfFileSigned() ?? $attestation->getPdfFileUnsigned();

        if (null === $path || !file_exists($path)) {
            throw $this->createNotFoundException('Le fichier PDF n\'est pas disponible.');
        }

        $response = new BinaryFileResponse($path);
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'attestation-'.$attestation->getReference().'.pdf');

        return $response;
    }

    #[Route('/attestation/{reference}/audit', name: 'app_attestation_audit', methods: ['GET'])]
    public function audit(
        string $reference,
        AttestationRepository $repository,
    ): Response {
        $attestation = $repository->findOneBy(['reference' => $reference]);

        if (null === $attestation) {
            throw $this->createNotFoundException('Attestation introuvable.');
        }

        $path = $attestation->getPdfFileCertificate();

        if (null === $path || !file_exists($path)) {
            throw $this->createNotFoundException('Le certificat de signature n\'est pas disponible.');
        }

        $response = new BinaryFileResponse($path);
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'certificat-'.$attestation->getReference().'.pdf');

        return $response;
    }
}
