<?php

/*
 * This file is part of the Serenity package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Controller\Admin;

use CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin\ContactType;
use CpCreation\VitiCore\Contact\Model\Contact;
use CpCreation\VitiCore\Contact\Repository\ContactRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;

#[IsGranted('ROLE_ADMIN')]
#[Route('/contact')]
class ContactController extends AbstractController
{
    public function __construct(private ContactRepository $contactRepository)
    {
    }

    #[Route('', name: 'admin_contact')]
    public function index(): Response
    {
        $contacts = $this->contactRepository->findBy([], ['createdAt' => 'DESC']);

        return $this->render('@CpCreationVitiCore/admin/contact/index.html.twig', [
            'items' => $contacts,
        ]);
    }

    #[Route('/new', name: 'admin_contact_new')]
    public function new(Request $request): Response
    {
        $form = $this->createForm(ContactType::class);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Contact $data */
            $data = $form->getData();
            $this->contactRepository->save($data);
            $this->addFlash('alert-success', 'Le contact '.$data->getCompany().' a été ajouté avec succès');

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

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

    #[Route('/{id}/edit', name: 'admin_contact_edit')]
    public function edit(Contact $contact, Request $request): Response
    {
        $form = $this->createForm(ContactType::class, $contact);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            /** @var Contact $data */
            $data = $form->getData();
            $data->setUpdatedAt(new \DateTime()); // Forcer Update de doctrine
            $this->contactRepository->save($data);
            $this->addFlash('alert-success', 'Le contact '.$data->getCompany().' a été modifié avec succès');

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

        return $this->render('@CpCreationVitiCore/admin/contact/edit.html.twig', [
            'item' => $contact,
            'form' => $form->createView(),
        ]);
    }

    #[Route('/{id}/delete', name: 'admin_contact_delete')]
    public function delete(Contact $contact): RedirectResponse
    {
        $this->contactRepository->delete($contact);
        $this->addFlash('alert-success', 'Le contact '.$contact->getCompany().' a été supprimé avec succès.');

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