<?php

namespace Functional;

use App\Entity\HardwareType;
use App\Factory\HardwareTypeFactory;
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 HardwareTypeTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string HW_TYPE_CREATE = 'test-hw-type-create';
    CONST string HW_TYPE_UPDATE = 'test-hw-type-update';
    CONST string HW_TYPE_DELETE = 'test-hw-type-delete';

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

        HardwareTypeFactory::createMany(10);

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

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

        $this->createClientWithCredentials()->request('POST', '/hardware-types',['json' => [
            'name' => self::HW_TYPE_CREATE,
        ]]);

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

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

        HardwareTypeFactory::createOne(['name' => self::HW_TYPE_CREATE]);
        $iri = $this->findIriBy(HardwareType::class, ['name' => self::HW_TYPE_CREATE]);

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

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

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

        HardwareTypeFactory::createOne(['name' => self::HW_TYPE_DELETE]);
        $iri = $this->findIriBy(HardwareType::class, ['name' => self::HW_TYPE_DELETE]);

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