<?php declare(strict_types=1);

namespace App\Service;

use App\Entity\User;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ConfigGenerator
{
    public function __construct(private UrlGeneratorInterface $urlGenerator)
    {
    }

    public function generateConfig(User $user): Response
    {
        // Define the configuration parameters
        $config = [
            'Version' => '15.0.0',
            'Name' => 'ventry-config-' . $user->getUsername(),
            'DestinationType' => 'ImageUploader, FileUploader',
            'RequestMethod' => 'POST',
            'RequestURL' => $this->urlGenerator->generate('app_upload_file', referenceType: 0),
            'Body' => 'MultipartFormData',
            'Arguments' => ['token' => $user->getToken()],
            'FileFormName' => 'file',
            'ThumbnailURL' => '{response}/thumbnail',
            'DeletionURL' => '{response}/delete',
            'ErrorMessage' => '{json:error}'
        ];

        // Generate a JSON representation of the configuration
        $jsonConfig = json_encode($config, JSON_PRETTY_PRINT);

        // Create a response with the JSON configuration
        $response = new Response($jsonConfig, Response::HTTP_OK);
        $response->headers->set(
            'Content-Disposition',
            "attachment; filename=\"ventry-config-{$user->getUsername()}.sxcu\""
        );

        return $response;
    }
}
