<?php

namespace App\Controller;

use App\Repository\AppLogRepository;
use App\Service\AppLoggerService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class LogVerifyController extends AbstractController
{
    #[Route('/admin/logs/verif/{id}/{hmac}', name: 'app_log_verify', methods: ['GET'])]
    public function __invoke(
        int $id,
        string $hmac,
        AppLogRepository $repository,
        AppLoggerService $loggerService,
    ): Response {
        $log = $repository->find($id);

        if (null === $log || !str_starts_with($log->getHmac(), $hmac)) {
            return $this->render('admin/logs/verify.html.twig', [
                'log' => null,
                'valid' => false,
                'id' => $id,
            ]);
        }

        $valid = $loggerService->verifyLog($log);

        return $this->render('admin/logs/verify.html.twig', [
            'log' => $log,
            'valid' => $valid,
            'id' => $id,
        ]);
    }
}
