<?php
declare(strict_types=1);

namespace App\Application\Newsletter\SignUp;

use App\Framework\Attributes\Route;
use App\Framework\CommandBus\CommandBus;
use App\Framework\CommandBus\DefaultCommandBus;
use App\Framework\Http\Method;
use App\Framework\Http\Status;
use App\Framework\Router\Result\ContentNegotiationResult;
use App\Framework\Router\Result\JsonResult;

final readonly class NewsletterSignup
{
    public function __construct(
        public CommandBus $commandBus,
    ) {}

    #[Route(path: '/newsletter/register', method: Method::POST)]
    public function __invoke(NewsletterSignupRequest $request): ContentNegotiationResult
    {
        // Den internen Command ausführen
        $command = new SignupUserToNewsletter($request->name, $request->email);

        $result = $this->commandBus->dispatch($command);


        // Hier könnten Sie das Ergebnis weiterverarbeiten oder loggen
        return new ContentNegotiationResult(
            jsonPayload: [
                'success' => true,
                'message' => 'Anmeldung erfolgreich!',
                'data' => [
                    $request->name,
                    $request->email,
                ],
            ],
            redirectTo: '/'
        );
    }
}
