<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Controller\Admin;

use App\Entity\Secteur;
use App\Entity\Site;
use App\Entity\TypeSav;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;

/**
 * @Route("/secteur")
 * @IsGranted("ROLE_USER")
 *
 */
class SecteurController extends ParentAdminController
{
    /**
     * MAJ agent adresse
     * @Route("/compare/secteur_site/{id}", methods="POST", name="compare_secteur_site")
     */
    public function compareSecteurSite(Site $siteCompare, Request $request) {
        $em = $this->getDoctrine()->getManager();
        $datas = $request->request->all();
        $response = [];

        if (isset($datas['coordinates'])){
            $secteurs = $em->getRepository(Secteur::class)->getSecteursFromCoordinates($datas['coordinates'][0],$datas['coordinates'][1]);
            foreach ($secteurs as $secteur) {
                if ($secteur->getSectorisationVersion()
                    && $secteur->getSectorisationVersion()->getEnProd() == true
                    && $secteur->getSectorisationVersion()->getSite() != $siteCompare) {
                    $site = $secteur->getSectorisationVersion()->getSite();
                    $agence = $site->getAgences()->first();
                    $response = ['error' => true, 'message' => 'Cette adresse fait partie d\'un secteur géré par le site de ' . $site->getNom() . ' ' . $agence->getNumTel()];
                }
            }
        }

        return new JsonResponse($response, Response::HTTP_OK);
    }

}