<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Healey\Robots\Robots;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class SeoController extends AbstractController
{
    #[Route(path: '/.well-known/security.txt',name: 'app_security_well_txt',methods: ['GET'])]
    #[Route(path: '/security.txt',name: 'app_security_txt',methods: ['GET'])]
    public function security(Request $request): Response
    {
        $r = new Response();
        $r->headers->set('Content-Type', 'text/plain');
        return $this->render('well-known/security.txt',[
            'path' => $request->getSchemeAndHttpHost()
        ],$r);
    }

    #[Route(path: '/robots.txt',name: 'app_seo_robots',methods: ['GET'])]
    public function robots(Request $request): Response
    {
        $robots = new Robots();

        $robots->addUserAgent("*");
        $robots->addHost($request->getHost());
        $robots->addSpacer();
        $robots->addAllow($this->generateUrl('app_home'));
        $robots->addAllow($this->generateUrl('app_about'));
        $robots->addAllow($this->generateUrl('app_members'));
        $robots->addAllow($this->generateUrl('app_events'));
        $robots->addAllow($this->generateUrl('app_contact'));

        $robots->addSpacer();
        $robots->addComment("Disallow page");
        $robots->addDisallow($this->generateUrl('app_legal'));
        $robots->addDisallow($this->generateUrl('app_rgpd'));
        $robots->addDisallow($this->generateUrl('app_hosting'));
        $robots->addDisallow($this->generateUrl('app_cookies'));
        $robots->addDisallow($this->generateUrl('app_cgu'));
        $robots->addDisallow($this->generateUrl('app_cgv'));
        $robots->addDisallow($this->generateUrl('app_login'));
        $robots->addDisallow($this->generateUrl('app_forgot_password'));

        $robots->addSpacer();
        $robots->addComment("Sitemap");
        $robots->addSitemap($this->generateUrl(
            'PrestaSitemapBundle_index', // Route name for the sitemap
            ['_format' => 'xml'],          // Parameters for the route
            UrlGeneratorInterface::ABSOLUTE_URL // Generate an absolute URL
        ));

        return new Response($robots->generate(),Response::HTTP_OK, array('Content-Type' => 'text/plain'));
    }
}
