<?php

namespace App\Controller\Frontend;

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

use App\Entity\Annonce;
use App\Repository\AnnonceRepository;
use App\Repository\PositionRepository;

#[Route('/frontend/annonce')]
class AnnonceController extends AbstractController
{
    /**
     * @var AnnonceRepository $annonceRepository
     */
    private $annonceRepository;

    /**
     * @var PositionRepository $positionRepository
     */
    private $positionRepository;
    

    /**
     * @param AnnonceRepository $annonceRepository
     * @param PositionRepository $positionRepository
     * @return $this
     */
    public function __construct(
        AnnonceRepository $annonceRepository,
        PositionRepository $positionRepository
    ){
        $this->annonceRepository = $annonceRepository;
        $this->positionRepository = $positionRepository;
    }



    #[Route('/index', name: 'frontend_annonce_index')]
    public function index(): Response
    {
        $annonces = [];

        $positions = $this->positionRepository->getPositionByDate(new \DateTime());
        
        foreach($positions as $position){
            $annonce = $position->getAnnonce();
            $annonces[$annonce->getId()] = $annonce;
        }
       
        return $this->render('frontend/annonce/index.html.twig', [
            'annonces' => $annonces,
        ]);
    }


    #[Route('/detail/{id}', name: 'frontend_annonce_detail')]
    public function detail(Annonce $annonce): Response
    {
    
        $detailtwig = 'frontend/annonce/detailSimpleImage.html.twig';

        if($annonce->getImageHome() && $annonce->getImageDetail1()){
            $detailtwig = 'frontend/annonce/detailManyImages.html.twig';
        }
        
        return $this->render($detailtwig, [ 'annonce' => $annonce ]);
    }


}