<?php

namespace Functional;

use App\Entity\UserGroup;
use App\Factory\UserFactory;
use App\Factory\UserGroupFactory;
use App\Model\UserGroupPermissions;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;

class UserGroupTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string USER_GROUP_CREATE = 'test-user-group-create';
    CONST string USER_GROUP_UPDATE = 'test-user-group-update';
    CONST string USER_GROUP_DELETE = 'test-user-group-delete';
    CONST string ROLE_ORGANIZATIONAL_UNIT_ADMIN = 'ROLE_ORGANIZATIONAL_UNIT_ADMIN';

    /**
     * @throws RedirectionExceptionInterface
     * @throws DecodingExceptionInterface
     * @throws ClientExceptionInterface
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function testGetCollectionUserGroup(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);

        UserGroupFactory::createOne(['name' => 'Super Admin', 'permissions' => ['ROLE_SUPER_ADMIN'], 'enabled' => true]);
        UserGroupFactory::createOne(['name' => 'Administrador de aulas', 'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_ADMIN'], 'enabled' => true]);
        UserGroupFactory::createOne(['name' => 'Operador de aulas', 'permissions' => ['ROLE_ORGANIZATIONAL_UNIT_OPERATOR'], 'enabled' => true]);
        UserGroupFactory::createOne(['name' => 'Usuario', 'permissions' => ['ROLE_USER'], 'enabled' => true]);

        $this->createClientWithCredentials()->request('GET', '/user-groups');
        $this->assertResponseStatusCodeSame(Response::HTTP_OK);
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
        $this->assertJsonContains([
            '@context' => '/contexts/UserGroup',
            '@id' => '/user-groups',
            '@type' => 'hydra:Collection',
            'hydra:totalItems' => 4,
        ]);
    }

    /**
     * @throws RedirectionExceptionInterface
     * @throws DecodingExceptionInterface
     * @throws ClientExceptionInterface
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function testCreateUserGroup(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
        $this->createClientWithCredentials()->request('POST', '/user-groups',['json' => [
            'name' => self::USER_GROUP_CREATE,
            'enabled' => true,
        ]]);

        $this->assertResponseStatusCodeSame(201);
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
        $this->assertJsonContains([
            '@context' => '/contexts/UserGroupOutput',
            '@type' => 'UserGroup',
            'name' => self::USER_GROUP_CREATE,
            'enabled' => true,
        ]);
    }

    /**
     * @throws RedirectionExceptionInterface
     * @throws DecodingExceptionInterface
     * @throws ClientExceptionInterface
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function testUpdateUserGroup(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);

        UserGroupFactory::createOne(['name' => self::USER_GROUP_UPDATE]);
        $iri = $this->findIriBy(UserGroup::class, ['name' => self::USER_GROUP_UPDATE]);

        $this->createClientWithCredentials()->request('PATCH', $iri, ['json' => [
            'name' => self::USER_GROUP_UPDATE,
            'enabled' => false
        ]]);

        $this->assertResponseIsSuccessful();
        $this->assertJsonContains([
            '@id' => $iri,
            'name' => self::USER_GROUP_UPDATE,
            'enabled' => false
        ]);
    }


    /**
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     * @throws RedirectionExceptionInterface
     * @throws DecodingExceptionInterface
     * @throws ClientExceptionInterface
     */
    public function testDeleteUser(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);
        UserGroupFactory::createOne(['name' => self::USER_GROUP_DELETE]);

        $iri = $this->findIriBy(UserGroup::class, ['name' => self::USER_GROUP_DELETE]);

        $this->createClientWithCredentials()->request('DELETE', $iri);
        $this->assertResponseStatusCodeSame(204);
        $this->assertNull(
            static::getContainer()->get('doctrine')->getRepository(UserGroup::class)->findOneBy(['name' => self::USER_GROUP_DELETE])
        );
    }
}