<?php

declare(strict_types=1);

namespace App\Tests\Functional\Controller;

use App\Module\Core\Domain\Entity\User;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * @internal
 */
final class ShareBrowseTest extends WebTestCase
{
    public function testBrowseRequiresAuthentication(): void
    {
        $client = self::createClient();
        $client->request('GET', '/api/share/browse?path=/');

        self::assertSame(401, $client->getResponse()->getStatusCode());
    }

    public function testBrowseRejectsPathTraversal(): void
    {
        $client = self::createClient();
        $this->login($client);

        $client->request('GET', '/api/share/browse?path='.urlencode('../etc'));

        self::assertSame(400, $client->getResponse()->getStatusCode());
    }

    public function testBrowseReturns409WhenNotConfigured(): void
    {
        $client = self::createClient();
        $this->login($client);

        $client->request('GET', '/api/share/browse?path=');

        self::assertSame(409, $client->getResponse()->getStatusCode());
    }

    public function testStatusReturnsDisabledByDefault(): void
    {
        $client = self::createClient();
        $this->login($client);

        $client->request('GET', '/api/share/status');

        self::assertResponseIsSuccessful();
        $data = json_decode($client->getResponse()->getContent(), true);
        self::assertFalse($data['enabled']);
    }

    private function login(KernelBrowser $client): void
    {
        $em   = self::getContainer()->get('doctrine.orm.entity_manager');
        $user = $em->getRepository(User::class)->findOneBy(['username' => 'alice']);
        $client->loginUser($user);
    }
}
