<?php

namespace App\Controller\FrontEnd;

use App\Entity\Photos;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

use Doctrine\ORM\EntityManagerInterface;

final class PhotosController extends AbstractController
{
    #[Route('/photos', name: 'front_end_photos_list', priority: 1)]
    public function index(EntityManagerInterface $entityManager): Response
    {   
        $photos = $entityManager->getRepository(Photos::class)->findAllOrderByLatest();

        $albums = [];

        foreach ($photos as $index => $photo) {
            $albums[] = [
                'title' => $photo->getTitle(),
                'date' => $photo->getDate(),
                'category' => $photo->getCategory()->getTitle(),
                'thumbnail' => 'files/uploads/photos/' . strtolower(str_replace(' ', '_', $photo->getTitle())) . '/' . $photo->getThumbnail(),
                'url' => $photo->getUrl(),    
            ];
        }

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

    #[Route('/photos/{url:photos}', name: 'front_end_photos_detail')]
    public function detail(Photos $photos): Response
    {   
        $album = [];
        $uploads = $photos->getUploads();
        
        $album = [
            'title' => $photos->getTitle(),
            'category' => $photos->getCategory()->getTitle(),
            'urlSafeCategory' => $photos->getCategory()->getUrlSafeTitle(),
            'tags' => $photos->getTags(),
            'text' => $photos->getText(),
            'description' => $photos->getDescription(),
            'date' => $photos->getDate(),
            'thumbnail' => '/files/uploads/photos/' . strtolower(str_replace(' ', '_', $photos->getTitle())) . '/' . $photos->getThumbnail(),
            'url' => $photos->getUrl(),
        ];

        foreach ($uploads as $index => $upload) {
            $filePath = 'files/uploads/photos/' . strtolower(str_replace(' ', '_', $album['title'])) . '/' . $upload->getFile();

            $album['uploads'][] = [
                'file' => $filePath,
                'alt' => $upload->getAltText(),
                'equipment' => $upload->getEquipment(),
                'caption' => $upload->getCaption(),
                'location' => $upload->getLocation(),
                'date' => $upload->getDate()
            ];
        }

        return $this->render('front/photos/detail.html.twig', [
            'photos' => $album
        ]);
    }
}

