<?php

namespace Functional;


use App\Entity\Hardware;
use App\Entity\HardwareType;
use App\Factory\HardwareFactory;
use App\Factory\HardwareTypeFactory;
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 HardwareTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string HW_CREATE = 'test-hw-create';
    CONST string HW_UPDATE = 'test-hw-update';
    CONST string HW_DELETE = 'test-hw-delete';
    CONST string HW_TYPE = 'Server';


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

        HardwareFactory::createMany(10);

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

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

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

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

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

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

        HardwareFactory::createOne(['name' => self::HW_CREATE]);
        $iri = $this->findIriBy(Hardware::class, ['name' => self::HW_CREATE]);

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

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

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

        HardwareFactory::createOne(['name' => self::HW_DELETE]);
        $iri = $this->findIriBy(Hardware::class, ['name' => self::HW_DELETE]);

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