<?php

namespace App\Tests\Controller;

use App\Service\RgpdService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class LegalControllerTest extends WebTestCase
{
    public function testMentionsLegales(): void
    {
        $client = static::createClient();
        $client->request('GET', '/mentions-legales');

        self::assertResponseIsSuccessful();
    }

    public function testCgu(): void
    {
        $client = static::createClient();
        $client->request('GET', '/cgu');

        self::assertResponseIsSuccessful();
    }

    public function testCgv(): void
    {
        $client = static::createClient();
        $client->request('GET', '/cgv');

        self::assertResponseIsSuccessful();
    }

    public function testHosting(): void
    {
        $client = static::createClient();
        $client->request('GET', '/hebergement');

        self::assertResponseIsSuccessful();
    }

    public function testCookies(): void
    {
        $client = static::createClient();
        $client->request('GET', '/cookies');

        self::assertResponseIsSuccessful();
    }

    public function testRgpd(): void
    {
        $client = static::createClient();
        $client->request('GET', '/rgpd');

        self::assertResponseIsSuccessful();
    }

    public function testConformite(): void
    {
        $client = static::createClient();
        $client->request('GET', '/conformite');

        self::assertResponseIsSuccessful();
    }

    public function testRgpdAccessEmptyFields(): void
    {
        $client = static::createClient();
        $client->request('POST', '/rgpd/acces', ['ip' => '', 'email' => '']);

        self::assertResponseRedirects('/rgpd');
    }

    public function testRgpdAccessWithDataFound(): void
    {
        $client = static::createClient();

        $mock = $this->createMock(RgpdService::class);
        $mock->method('handleAccessRequest')
            ->with('192.168.1.1', 'test@example.com')
            ->willReturn(['found' => true, 'count' => 5]);

        static::getContainer()->set(RgpdService::class, $mock);

        $client->request('POST', '/rgpd/acces', ['ip' => '192.168.1.1', 'email' => 'test@example.com']);

        self::assertResponseRedirects('/rgpd');
    }

    public function testRgpdAccessWithNoData(): void
    {
        $client = static::createClient();

        $mock = $this->createMock(RgpdService::class);
        $mock->method('handleAccessRequest')
            ->with('192.168.1.1', 'test@example.com')
            ->willReturn(['found' => false, 'count' => 0]);

        static::getContainer()->set(RgpdService::class, $mock);

        $client->request('POST', '/rgpd/acces', ['ip' => '192.168.1.1', 'email' => 'test@example.com']);

        self::assertResponseRedirects('/rgpd');
    }

    public function testRgpdDeletionEmptyFields(): void
    {
        $client = static::createClient();
        $client->request('POST', '/rgpd/suppression', ['ip' => '', 'email' => '']);

        self::assertResponseRedirects('/rgpd');
    }

    public function testRgpdDeletionWithDataFound(): void
    {
        $client = static::createClient();

        $mock = $this->createMock(RgpdService::class);
        $mock->method('handleDeletionRequest')
            ->with('192.168.1.1', 'test@example.com')
            ->willReturn(['found' => true, 'count' => 3]);

        static::getContainer()->set(RgpdService::class, $mock);

        $client->request('POST', '/rgpd/suppression', ['ip' => '192.168.1.1', 'email' => 'test@example.com']);

        self::assertResponseRedirects('/rgpd');
    }

    public function testRgpdDeletionWithNoData(): void
    {
        $client = static::createClient();

        $mock = $this->createMock(RgpdService::class);
        $mock->method('handleDeletionRequest')
            ->with('192.168.1.1', 'test@example.com')
            ->willReturn(['found' => false, 'count' => 0]);

        static::getContainer()->set(RgpdService::class, $mock);

        $client->request('POST', '/rgpd/suppression', ['ip' => '192.168.1.1', 'email' => 'test@example.com']);

        self::assertResponseRedirects('/rgpd');
    }
}
