<?php

declare(strict_types=1);

namespace App\Tests\Functional\Controller\Mail;

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

/**
 * @internal
 */
class MailSettingsControllerTest extends WebTestCase
{
    public function testGetConfigurationReturns401WhenNotAuthenticated(): void
    {
        $client = static::createClient();
        $client->request('GET', '/api/mail/configuration');

        self::assertResponseStatusCodeSame(401);
    }

    public function testGetConfigurationReturns403ForRoleUser(): void
    {
        $client    = static::createClient();
        $container = static::getContainer();
        $em        = $container->get('doctrine.orm.entity_manager');

        $user = $em->getRepository(User::class)->findOneBy(['username' => 'alice']);
        $client->loginUser($user);
        $client->request('GET', '/api/mail/configuration');

        self::assertResponseStatusCodeSame(403);
    }

    public function testGetConfigurationReturns200ForAdmin(): void
    {
        $client    = static::createClient();
        $container = static::getContainer();
        $em        = $container->get('doctrine.orm.entity_manager');

        $admin = $em->getRepository(User::class)->findOneBy(['username' => 'admin']);
        $client->loginUser($admin);
        $client->request('GET', '/api/mail/configuration');

        self::assertResponseIsSuccessful();
        $data = json_decode($client->getResponse()->getContent(), true);

        self::assertArrayNotHasKey('password', $data);
        self::assertArrayNotHasKey('encryptedPassword', $data);
        self::assertArrayHasKey('hasPassword', $data);
        self::assertArrayHasKey('imapHost', $data);
        self::assertArrayHasKey('enabled', $data);
    }

    public function testPatchConfigurationReturns403ForRoleUser(): void
    {
        $client    = static::createClient();
        $container = static::getContainer();
        $em        = $container->get('doctrine.orm.entity_manager');

        $user = $em->getRepository(User::class)->findOneBy(['username' => 'alice']);
        $client->loginUser($user);
        $client->request(
            'PATCH',
            '/api/mail/configuration',
            [],
            [],
            ['CONTENT_TYPE' => 'application/merge-patch+json'],
            json_encode(['enabled' => false])
        );

        self::assertResponseStatusCodeSame(403);
    }

    public function testPatchConfigurationUpdatesFieldsForAdmin(): void
    {
        $client    = static::createClient();
        $container = static::getContainer();
        $em        = $container->get('doctrine.orm.entity_manager');

        $admin = $em->getRepository(User::class)->findOneBy(['username' => 'admin']);
        $client->loginUser($admin);
        $client->request(
            'PATCH',
            '/api/mail/configuration',
            [],
            [],
            ['CONTENT_TYPE' => 'application/merge-patch+json'],
            json_encode(['imapHost' => 'imap.example.com', 'enabled' => false])
        );

        self::assertResponseIsSuccessful();
        $data = json_decode($client->getResponse()->getContent(), true);
        self::assertSame('imap.example.com', $data['imapHost']);
        self::assertArrayNotHasKey('password', $data);
    }

    public function testPatchConfigurationWithPasswordEncryptsIt(): void
    {
        $client    = static::createClient();
        $container = static::getContainer();
        $em        = $container->get('doctrine.orm.entity_manager');

        $admin = $em->getRepository(User::class)->findOneBy(['username' => 'admin']);
        $client->loginUser($admin);
        $client->request(
            'PATCH',
            '/api/mail/configuration',
            [],
            [],
            ['CONTENT_TYPE' => 'application/merge-patch+json'],
            json_encode(['password' => 'secret123'])
        );

        self::assertResponseIsSuccessful();
        $data = json_decode($client->getResponse()->getContent(), true);
        self::assertTrue($data['hasPassword']);
        self::assertArrayNotHasKey('password', $data);
    }
}
