<?php

namespace Functional;

use App\Entity\Command;
use App\Entity\CommandTask;
use App\Entity\OrganizationalUnit;
use App\Factory\CommandFactory;
use App\Factory\CommandTaskFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\UserFactory;
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 CommandTaskTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';

    CONST string CMD_TASK_CREATE = 'test-cmd-task-create';
    CONST string CMD_TASK_UPDATE = 'test-cmd-task-update';
    CONST string CMD_TASK_DELETE = 'test-cmd-task-delete';
    CONST string CMD_CREATE = 'test-cmd-create';

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

        CommandTaskFactory::createMany(10);

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

    /**
     * @throws RedirectionExceptionInterface
     * @throws DecodingExceptionInterface
     * @throws ClientExceptionInterface
     * @throws TransportExceptionInterface
     * @throws ServerExceptionInterface
     */
    public function testCreateCommandTask(): 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]);

        $this->createClientWithCredentials()->request('POST', '/command-tasks',['json' => [
            'name' => self::CMD_TASK_CREATE,
            'organizationalUnit' => $ouIri,
            'scope' => 'organizational-unit',
        ]]);

        $this->assertResponseStatusCodeSame(201);
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
        $this->assertJsonContains([
            '@context' => '/contexts/CommandTaskOutput',
            '@type' => 'CommandTask',
            'name' => self::CMD_TASK_CREATE,
            'scope' => 'organizational-unit',
        ]);
    }

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

        CommandTaskFactory::createOne(['name' => self::CMD_TASK_CREATE]);
        $iri = $this->findIriBy(CommandTask::class, ['name' => self::CMD_TASK_CREATE]);

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

        $this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
            'name' => self::CMD_TASK_UPDATE,
            'organizationalUnit' => $ouIri,
            'scope' => 'organizational-unit',
        ]]);

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

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

        $ou = OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]);

        CommandTaskFactory::createOne(['name' => self::CMD_TASK_DELETE, 'organizationalUnit' => $ou, 'scope' => 'organizational-unit']);
        $iri = $this->findIriBy(CommandTask::class, ['name' => self::CMD_TASK_DELETE]);

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