<?php

namespace App\Controller;

use App\Service\RgpdService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class LegalController extends AbstractController
{
    #[Route('/mentions-legales', name: 'app_mentions_legales')]
    public function mentionsLegales(): Response
    {
        return $this->render('legal/mentions_legales.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Mentions legales', 'url' => '/mentions-legales'],
            ],
        ]);
    }

    #[Route('/cgu', name: 'app_cgu')]
    public function cgu(): Response
    {
        return $this->render('legal/cgu.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'CGU', 'url' => '/cgu'],
            ],
        ]);
    }

    #[Route('/cgv', name: 'app_cgv')]
    public function cgv(): Response
    {
        return $this->render('legal/cgv.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'CGV', 'url' => '/cgv'],
            ],
        ]);
    }

    #[Route('/hebergement', name: 'app_hosting')]
    public function hosting(): Response
    {
        return $this->render('legal/hosting.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Hebergement', 'url' => '/hebergement'],
            ],
        ]);
    }

    #[Route('/cookies', name: 'app_cookies')]
    public function cookies(): Response
    {
        return $this->render('legal/cookies.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Politique de cookies', 'url' => '/cookies'],
            ],
        ]);
    }

    #[Route('/rgpd', name: 'app_rgpd', methods: ['GET'])]
    public function rgpd(Request $request): Response
    {
        return $this->render('legal/rgpd.html.twig', [
            'client_ip' => $request->getClientIp(),
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Politique RGPD', 'url' => '/rgpd'],
            ],
        ]);
    }

    #[Route('/rgpd/acces', name: 'app_rgpd_access', methods: ['POST'])]
    public function rgpdAccess(Request $request, RgpdService $rgpd): Response
    {
        $ip = trim($request->request->getString('ip'));
        $email = trim($request->request->getString('email'));

        if ('' === $ip || '' === $email) {
            $this->addFlash('error', 'Veuillez remplir tous les champs.');

            return $this->redirectToRoute('app_rgpd');
        }

        $result = $rgpd->handleAccessRequest($ip, $email);

        if ($result['found']) {
            $this->addFlash('success', 'Vos donnees ont ete envoyees par email.');
        } else {
            $this->addFlash('success', 'Aucune donnee trouvee pour cette adresse IP. Un email de confirmation a ete envoye.');
        }

        return $this->redirectToRoute('app_rgpd');
    }

    #[Route('/rgpd/suppression', name: 'app_rgpd_deletion', methods: ['POST'])]
    public function rgpdDeletion(Request $request, RgpdService $rgpd): Response
    {
        $ip = trim($request->request->getString('ip'));
        $email = trim($request->request->getString('email'));

        if ('' === $ip || '' === $email) {
            $this->addFlash('error', 'Veuillez remplir tous les champs.');

            return $this->redirectToRoute('app_rgpd');
        }

        $result = $rgpd->handleDeletionRequest($ip, $email);

        if ($result['found']) {
            $this->addFlash('success', 'Vos donnees ont ete supprimees. Une attestation a ete envoyee par email.');
        } else {
            $this->addFlash('success', 'Aucune donnee trouvee pour cette adresse IP.');
        }

        return $this->redirectToRoute('app_rgpd');
    }

    #[Route('/conformite', name: 'app_conformite')]
    public function conformite(): Response
    {
        return $this->render('legal/conformite.html.twig', [
            'breadcrumbs' => [
                ['name' => 'Accueil', 'url' => '/'],
                ['name' => 'Conformite', 'url' => '/conformite'],
            ],
        ]);
    }
}
