<?php

namespace App\Controller;

use App\Entity\Collection;
use App\Entity\Bibliography;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class CollectionController extends AbstractController
{
    #[Route('/collection/{id<\d+>}', name: 'app_collection')]
    public function index(Collection $collection, EntityManagerInterface $em): Response
    {
        $repo = $em->getRepository(Bibliography::class);
        $bibliographies = $repo->findAllByCollection($collection->getId());

        $collection->setBibliographies($bibliographies);

        return $this->render('collection/index.html.twig', [
            'controller_name' => 'CollectionController',
            'record' => $collection,
        ]);
    }

    #[Route('/collection', name: 'app_collection_landing')]
    public function landing(EntityManagerInterface $em): Response
    {
        $repo = $em->getRepository(Collection::class);
        $records = $repo->findBy([], ['modifiedAt' => 'DESC']);
        $count = count($records);

        $records = array_slice($records, 0, 15);

        return $this->render('collection/landing.html.twig', [
            'controller_name' => 'CollectionController',
            'records' => $records,
            'count' => $count,
        ]);
    }
}
