<?php
namespace App\Controller;

use App\Entity\Note;
use App\Form\Type\NoteType;
use DateTime;
use DateTimeZone;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Attribute\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use function Symfony\Component\HttpFoundation\Response;

class GuiController extends AbstractController {
    #[Route('/', name: 'index')]
    #[Template('/index.html.twig')]
    public function index(): array {
        return [
            'title' => 'Joe Carstairs',
            'description' => 'Joe Carstairs\' personal website',
        ];
    }

    #[Route('/notes', name: 'notes')]
    #[Template('/notes.html.twig')]
    public function notes(
        EntityManagerInterface $entityManager,
    ): array {
        $notes = $entityManager->getRepository(Note::class)->findAllOrderedBySlugDesc();

        return [
            'title' => 'Joe Carstairs\' notes',
            'description' => 'Joe Carstairs\' notes',
            'notes' => $notes,
        ];
    }

    #[IsGranted('ROLE_EDITOR')]
    #[Route('/notes/write')]
    #[Template('/write_note.html.twig')]
    public function writeNote(
        EntityManagerInterface $entityManager,
        Request $request,
    ): Response {
        $note = new Note();
        $form = $this->createForm(NoteType::class, $note);

        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $note = $form->getData();

            $now = new DateTime('now');
            $note->setPublishedDate($now);

            $num_existing_notes_today = $entityManager->getRepository(Note::class)->countWherePublishedOnDate($now);
            $note->getPublishedDate()->setTimezone(new DateTimeZone('Europe/London'));
            $slug = $note->getPublishedDate()->format('Y-m-d');
            if ($num_existing_notes_today > 0) {
                $slug = $slug . '-' . $num_existing_notes_today;
            }
            $note->setSlug($slug);

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

            return $this->redirectToRoute('note', ['slug' => $slug]);
        }

        return $this->render('/write_note.html.twig', [
            'title' => 'Write for Joe Carstairs',
            'description' => 'The authoring page for Joe Carstairs\' personal website',
            'form' => $form,
        ]);
    }

    /**
     * @return array<string,mixed>
     */
    #[Route('/notes/{slug}', name: 'note')]
    #[Template('/note.html.twig')]
    public function note(
        EntityManagerInterface $entityManager,
        string $slug,
    ): array {
        $repository = $entityManager->getRepository(Note::class);
        $note = $repository->findOneBy(['slug' => $slug]);

        return [
            'title' => 'Joe Carstairs\' notes',
            'description' => 'Joe Carstairs\' notes',
            'note' => $note,
            'slug' => $slug,
        ];
    }
}
