<?php

namespace Functional;


use App\Entity\Software;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\SoftwareFactory;
use App\Factory\UserFactory;
use App\Model\SoftwareTypes;
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 SoftwareTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string SW_CREATE = 'test-sw-create';
    CONST string SW_UPDATE = 'test-sw-update';
    CONST string SW_DELETE = 'test-sw-delete';

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

        SoftwareFactory::createMany(10);

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

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

        $this->createClientWithCredentials()->request('POST', '/software',['json' => [
            'name' => self::SW_CREATE,
            'type' => SoftwareTypes::FILE
        ]]);

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

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

        SoftwareFactory::createOne(['name' => self::SW_CREATE]);
        $iri = $this->findIriBy(Software::class, ['name' => self::SW_CREATE]);

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

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

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

        SoftwareFactory::createOne(['name' => self::SW_DELETE]);
        $iri = $this->findIriBy(Software::class, ['name' => self::SW_DELETE]);

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