<?php

namespace Functional;


use App\Entity\View;
use App\Factory\UserFactory;
use App\Factory\ViewFactory;
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 ViewTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string VIEW_CREATE = 'test-view-create';
    CONST string VIEW_UPDATE = 'test-view-update';
    CONST string VIEW_DELETE = 'test-view-delete';

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

        ViewFactory::createMany(10, ['user' => $user]);

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

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

        ViewFactory::createOne(['name' => self::VIEW_CREATE, 'favourite' => true, 'user' => $user]);
        $viewIri = $this->findIriBy(View::class, ['name' => self::VIEW_CREATE]);

        $this->createClientWithCredentials()->request('POST', '/views',['json' => [
            'name' => self::VIEW_CREATE,
            'favourite' => true
        ]]);

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

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

        ViewFactory::createOne(['name' => self::VIEW_CREATE, 'favourite' => true, 'user' => $user]);
        $viewIri = $this->findIriBy(View::class, ['name' => self::VIEW_CREATE]);

        $this->createClientWithCredentials()->request('PUT', $viewIri, ['json' => [
            'name' => self::VIEW_UPDATE,
            'favourite' => false
        ]]);

        $this->assertResponseIsSuccessful();
        $this->assertJsonContains([
            '@id' => $viewIri,
            'name' => self::VIEW_UPDATE,
            'favorite' => false
        ]);
    }

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

        ViewFactory::createOne(['name' => self::VIEW_DELETE, 'favourite' => true, 'user' => $user]);
        $viewIri = $this->findIriBy(View::class, ['name' => self::VIEW_DELETE]);

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