<?php

namespace App\Tests\Controller;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SitemapControllerTest extends WebTestCase
{
    public function testSitemapIndexReturnsXml(): void
    {
        $client = static::createClient();
        $client->request('GET', '/sitemap.xml');

        self::assertResponseIsSuccessful();
        self::assertStringContainsString('text/xml', $client->getResponse()->headers->get('Content-Type'));
    }

    public function testSitemapMainReturnsXml(): void
    {
        $client = static::createClient();
        $client->request('GET', '/sitemap-main.xml');

        self::assertResponseIsSuccessful();
        self::assertStringContainsString('text/xml', $client->getResponse()->headers->get('Content-Type'));
        self::assertStringContainsString('/tarifs', $client->getResponse()->getContent());
    }

    public function testSitemapEventsReturnsXml(): void
    {
        $client = static::createClient();
        $client->request('GET', '/sitemap-events-1.xml');

        self::assertResponseIsSuccessful();
        self::assertStringContainsString('text/xml', $client->getResponse()->headers->get('Content-Type'));
    }

    public function testSitemapOrgasReturnsXml(): void
    {
        $client = static::createClient();
        $client->request('GET', '/sitemap-orgas-1.xml');

        self::assertResponseIsSuccessful();
        self::assertStringContainsString('text/xml', $client->getResponse()->headers->get('Content-Type'));
    }

    public function testSitemapOrgasIncludesLogoImage(): void
    {
        $client = static::createClient();
        $em = static::getContainer()->get(EntityManagerInterface::class);

        $organizer = new User();
        $organizer->setEmail('test-sitemap-logo-'.uniqid().'@example.com');
        $organizer->setFirstName('Logo');
        $organizer->setLastName('Test');
        $organizer->setPassword('hashed');
        $organizer->setRoles(['ROLE_ORGANIZER']);
        $organizer->setIsApproved(true);
        $organizer->setIsVerified(true);
        $organizer->setCompanyName('Asso Logo');
        $organizer->setLogoName('test-logo.png');
        $em->persist($organizer);
        $em->flush();

        $client->request('GET', '/sitemap-orgas-1.xml');

        self::assertResponseIsSuccessful();
        $content = $client->getResponse()->getContent();
        self::assertStringContainsString('image:image', $content);
        self::assertStringContainsString('test-logo.png', $content);
        self::assertStringContainsString('Asso Logo', $content);
    }

    public function testSitemapEventsIncludesEventImage(): void
    {
        $client = static::createClient();
        $em = static::getContainer()->get(EntityManagerInterface::class);

        $organizer = new User();
        $organizer->setEmail('test-sitemap-event-img-'.uniqid().'@example.com');
        $organizer->setFirstName('Img');
        $organizer->setLastName('Event');
        $organizer->setPassword('hashed');
        $organizer->setRoles(['ROLE_ORGANIZER']);
        $organizer->setIsApproved(true);
        $organizer->setIsVerified(true);
        $organizer->setCompanyName('Asso Img');
        $em->persist($organizer);

        $event = new \App\Entity\Event();
        $event->setAccount($organizer);
        $event->setTitle('Event With Image');
        $event->setStartAt(new \DateTimeImmutable('2026-08-01 10:00'));
        $event->setEndAt(new \DateTimeImmutable('2026-08-01 18:00'));
        $event->setAddress('1 rue');
        $event->setZipcode('75001');
        $event->setCity('Paris');
        $event->setIsOnline(true);
        $event->setEventMainPictureName('event-poster.jpg');
        $em->persist($event);
        $em->flush();

        $client->request('GET', '/sitemap-events-1.xml');

        self::assertResponseIsSuccessful();
        $content = $client->getResponse()->getContent();
        self::assertStringContainsString('image:image', $content);
        self::assertStringContainsString('event-poster.jpg', $content);
    }
}
