<?php

namespace App\Controller;

use App\DataObjects\ContactData;
use App\Form\ContactForm;
use App\Service\ContactService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

final class ContactController extends AbstractController
{
    #[Route(path: '/contact', name: 'app_contact', methods: ['GET', 'POST'])]
    public function index(Request $request, ContactService $service): Response
    {
        $data = new ContactData();
        $form = $this->createForm(ContactForm::class, $data)->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $service->send($data);
            noty()->success('Deine Nachricht wurde erfolgreich versendet!');

            return $this->redirectToRoute('app_contact');
        }

        return $this->render('contact/index.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}
