<?php

namespace Functional;

use App\Entity\OperativeSystem;
use App\Factory\OperativeSystemFactory;
use App\Factory\OrganizationalUnitFactory;
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 OperativeSystemTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string OS_CREATE = 'test-os-create';
    CONST string OS_UPDATE = 'test-os-update';
    CONST string OS_DELETE = 'test-os-delete';

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

        OperativeSystemFactory::createMany(10);

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

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

        $this->createClientWithCredentials()->request('POST', '/operative-systems',['json' => [
            'name' => self::OS_CREATE,
        ]]);

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

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

        OperativeSystemFactory::createOne(['name' => self::OS_CREATE]);
        $iri = $this->findIriBy(OperativeSystem::class, ['name' => self::OS_CREATE]);

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

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

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

        OperativeSystemFactory::createOne(['name' => self::OS_DELETE]);
        $iri = $this->findIriBy(OperativeSystem::class, ['name' => self::OS_DELETE]);

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