<?php

namespace Functional;

use App\Entity\ImageRepository;
use App\Factory\ImageRepositoryFactory;
use App\Factory\UserFactory;
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 ImageRepositoryTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string REPOSITORY_CREATE = 'test-repository-create';
    CONST string REPOSITORY_UPDATE = 'test-repository-update';
    CONST string REPOSITORY_DELETE = 'test-repository-delete';

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

        ImageRepositoryFactory::createMany(10);

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

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

        $this->createClientWithCredentials()->request('POST', '/image-repositories',['json' => [
            'name' => self::REPOSITORY_CREATE,
            'comments' => self::REPOSITORY_CREATE,
            'ip' => '127.0.0.1'
        ]]);

        $this->assertResponseStatusCodeSame(201);
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
        $this->assertJsonContains([
            '@context' => '/contexts/ImageRepositoryOutput',
            '@type' => 'ImageRepository',
            'name' => self::REPOSITORY_CREATE,
            'comments' => self::REPOSITORY_CREATE,
            'ip' => '127.0.0.1'
        ]);
    }

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

        ImageRepositoryFactory::createOne(['name' => self::REPOSITORY_UPDATE, 'comments' => self::REPOSITORY_UPDATE]);
        $iri = $this->findIriBy(ImageRepository::class, ['name' => self::REPOSITORY_UPDATE]);

        $this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
            'name' => self::REPOSITORY_UPDATE,
            'comments' => self::REPOSITORY_UPDATE,
            'ip' => '127.0.0.1'
        ]]);

        $this->assertResponseIsSuccessful();
        $this->assertJsonContains([
            '@id' => $iri,
            'name' => self::REPOSITORY_UPDATE,
            'comments' => self::REPOSITORY_UPDATE,
            'ip' => '127.0.0.1'
        ]);
    }

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

        ImageRepositoryFactory::createOne(['name' => self::REPOSITORY_DELETE, 'comments' => self::REPOSITORY_DELETE]);
        $iri = $this->findIriBy(ImageRepository::class, ['name' => self::REPOSITORY_DELETE]);
        $this->createClientWithCredentials()->request('DELETE', $iri);
        $this->assertResponseStatusCodeSame(204);
        $this->assertNull(
            static::getContainer()->get('doctrine')->getRepository(ImageRepository::class)->findOneBy(['name' => self::REPOSITORY_DELETE])
        );
    }
}