<?php

namespace Functional;

use App\Entity\Client;
use App\Entity\PxeTemplate;
use App\Entity\User;
use App\Factory\PxeTemplateFactory;
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 PxeTemplateTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string PXE_TEMPLATE_CREATE = 'test-pxetemplate-create';
    CONST string PXE_TEMPLATE_UPDATE = 'test-pxetemplate-update';
    CONST string PXE_TEMPLATE_DELETE = 'test-pxetemplate-delete';

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

        PxeTemplateFactory::createMany(10);

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

    /*
    public function testCreatePxeTemplate(): void
    {

        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);

        $this->createClientWithCredentials()->request('POST', '/pxe-templates',['json' => [
            'name' => self::PXE_TEMPLATE_CREATE,
            'templateContent' => 'content'
        ]]);

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

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

        PxeTemplateFactory::createOne(['name' => self::PXE_TEMPLATE_CREATE, 'templateContent' => 'content']);
        $iri = $this->findIriBy(PxeTemplate::class, ['name' => self::PXE_TEMPLATE_CREATE]);

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

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

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

        PxeTemplateFactory::createOne(['name' => self::PXE_TEMPLATE_DELETE, 'templateContent' => 'content']);
        $iri = $this->findIriBy(PxeTemplate::class, ['name' => self::PXE_TEMPLATE_DELETE]);

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