<?php

namespace App\Controller;

use App\Entity\Collector;
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 CollectorController extends AbstractController
{
    #[Route('/collector/{id<\d+>}', name: 'app_collector')]
    public function index(Collector $collector, EntityManagerInterface $em): Response
    {
        $repo = $em->getRepository(Bibliography::class);
        $bibliographies = $repo->findAllByCollector($collector->getId());
        $collector->setBibliographies($bibliographies);

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

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

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

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