<?php

namespace App\Controller;

use App\Entity\Message;
use App\Entity\UserApp;
use App\Form\MessageType;
use App\Repository\MessageRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/message')]
final class MessageController extends AbstractController
{
    #[Route(name: 'app_message_index', methods: ['GET'])]
    public function index(MessageRepository $messageRepository): Response
    {
        $user = $this->getUser();
        $messages = $messageRepository->findLatestMessagesByUser($user);

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

    #[Route('/conversation/{id}', name: 'app_message_conversation', methods: ['GET'])]
    public function conversation(Message $message, MessageRepository $messageRepository): Response
    {
        $user = $this->getUser();
        $otherUser = $message->getSender() === $user ? $message->getReceiver() : $message->getSender();

        $conversation = $messageRepository->findByConversation($user, $otherUser);

        return $this->render('message/conversation.html.twig', [
            'conversation' => $conversation,
            'receiverId' => $otherUser->getId(),
            'receiver' => $otherUser,
        ]);
    }


    #[Route('/new/{receiverId}', name: 'app_message_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager, ?int $receiverId = null, UserRepository $userRepository): Response
    {
        $message = new Message();

        if ($receiverId) {
            $receiver = $userRepository->find($receiverId);
            if ($receiver) {
                $message->setReceiver($receiver);
            }
        }

        $form = $this->createForm(MessageType::class, $message);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $message->setSendingDate(new \DateTime());
            $message->setSender($this->getUser());

            $entityManager->persist($message);
            $entityManager->flush();

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

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

    #[Route('/{id}', name: 'app_message_show', methods: ['GET'])]
    public function show(Message $message): Response
    {
        return $this->render('message/show.html.twig', [
            'message' => $message,
        ]);
    }

    #[Route('/{id}/edit', name: 'app_message_edit', methods: ['GET', 'POST'])]
    public function edit(Request $request, Message $message, EntityManagerInterface $entityManager): Response
    {
        $form = $this->createForm(MessageType::class, $message);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $message->setSendingDate(new \DateTime());
            $entityManager->flush();

            return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER);
        }

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

    #[Route('/{id}', name: 'app_message_delete', methods: ['POST'])]
    public function delete(Request $request, Message $message, EntityManagerInterface $entityManager): Response
    {
        if ($this->isCsrfTokenValid('delete'.$message->getId(), $request->getPayload()->getString('_token'))) {
            $entityManager->remove($message);
            $entityManager->flush();
        }

        return $this->redirectToRoute('app_message_index', [], Response::HTTP_SEE_OTHER);
    }


}
