<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

final class SpaController
{
    #[Route('/app/{path}', name: 'app_spa', requirements: ['path' => '.*'], defaults: ['path' => ''], methods: ['GET'])]
    public function __invoke(): BinaryFileResponse|JsonResponse
    {
        $index = dirname(__DIR__, 2).'/public/app/index.html';
        if (!is_file($index)) {
            return new JsonResponse(['error' => 'frontend_not_built'], 503);
        }

        $response = new BinaryFileResponse($index);
        $response->headers->set('Cache-Control', 'no-store');

        return $response;
    }
}
