<?php

namespace Functional;

use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\HardwareType;
use App\Entity\OrganizationalUnit;
use App\Entity\User;
use App\Entity\UserGroup;
use App\Factory\ClientFactory;
use App\Factory\HardwareProfileFactory;
use App\Factory\HardwareTypeFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\UserFactory;
use App\Factory\UserGroupFactory;
use App\Model\OrganizationalUnitTypes;
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 ClientTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string CLIENT_CREATE = 'test-client-create';
    CONST string CLIENT_UPDATE = 'test-client-update';
    CONST string CLIENT_DELETE = 'test-client-delete';
    const string HW_PROFILE = 'HW Test';

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

        $ou = OrganizationalUnitFactory::new(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT])->create()->_disableAutoRefresh();
        $ou->_save();
        ClientFactory::createOne(['organizationalUnit' => $ou]);

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

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

        OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
        $ouIri = $this->findIriBy(OrganizationalUnit::class, ['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);

        HardwareProfileFactory::createOne(['description' => self::HW_PROFILE]);
        $hpIri = $this->findIriBy(HardwareProfile::class, ['description' => self::HW_PROFILE]);

        $this->createClientWithCredentials()->request('POST', '/clients',['json' => [
            'name' => self::CLIENT_CREATE,
            'organizationalUnit' => $ouIri,
            'hardwareProfile' => $hpIri,
            'serialNumber' => '123abc',
        ]]);

        $this->assertResponseStatusCodeSame(201);
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
        $this->assertJsonContains([
            '@context' => '/contexts/ClientOutput',
            '@type' => 'Client',
            'name' => self::CLIENT_CREATE,
            'serialNumber' => '123abc',
        ]);
    }

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

        $ou = OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
        $hp = HardwareProfileFactory::createOne(['description' => self::HW_PROFILE]);

        ClientFactory::createOne(['name' => self::CLIENT_UPDATE, 'serialNumber' => '123abc', 'organizationalUnit' => $ou, 'hardwareProfile' => $hp]);
        $iri = $this->findIriBy(Client::class, ['name' => self::CLIENT_UPDATE]);

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

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

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

        $ou = OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);
        $hp = HardwareProfileFactory::createOne(['description' => self::HW_PROFILE]);

        ClientFactory::createOne(['name' => self::CLIENT_DELETE, 'serialNumber' => '123abc', 'organizationalUnit' => $ou, 'hardwareProfile' => $hp]);
        $iri = $this->findIriBy(Client::class, ['name' => self::CLIENT_DELETE]);

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