<?php

namespace App\Controller;

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

class SiteController extends AbstractController
{
    #[Route('/site', name: 'app_site')]
    public function index(EntityManagerInterface $em): Response
    {
        $repo = $em->getRepository(Site::class);
        $sites = $repo->findBy([], ['id' => 'DESC']);
        $count = count($sites);
        $sites = array_slice($sites, 0, 15);

        return $this->render('site/index.html.twig', [
            'controller_name' => 'SiteController',
            'records' => $sites,
            'count' => $count,
        ]);
    }
    #[Route('/site/{id<\d+>}', name: 'app_site_view')]
    public function view(Site $site, EntityManagerInterface $em): Response
    {
        $repo = $em->getRepository(Site::class);
        $coords = $repo->coordinates($site->getId());

        $site->setLat($coords['lat']);
        $site->setLng($coords['lng']);

        return $this->render('site/view.html.twig', [
            'controller_name' => 'SiteController',
            'record' => $site,
        ]);
    }
    /**
     * @todo Implement...
     */
    #[Route('/site/copy/{id<\d+>}', name: 'app_site_copy')]
    public function copy(Site $site, EntityManagerInterface $em): Response
    {
        return $this->render('site/index.html.twig', [
            'controller_name' => 'SiteController',
            'record' => $site,
        ]);
    }
    /**
     * @todo Implement...
     */
    #[Route('/site/delete/{id<\d+>}', name: 'app_site_del')]
    public function delete(Site $site, EntityManagerInterface $em): Response
    {
        return $this->render('site/index.html.twig', [
            'controller_name' => 'SiteController',
            'record' => $site,
        ]);
    }
}
