<?php

namespace Functional;

use App\Entity\Client;
use App\Entity\HardwareProfile;
use App\Entity\Menu;
use App\Entity\OrganizationalUnit;
use App\Entity\User;
use App\Factory\ClientFactory;
use App\Factory\HardwareProfileFactory;
use App\Factory\MenuFactory;
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 MenuTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string MENU_CREATE = 'test-menu-create';
    CONST string MENU_UPDATE = 'test-menu-update';
    CONST string MENU_DELETE = 'test-menu-delete';

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

        MenuFactory::createMany(10);

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

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

        $this->createClientWithCredentials()->request('POST', '/menus',['json' => [
            'name' => self::MENU_CREATE,
            'resolution' => "1920x1080",
        ]]);

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

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

        MenuFactory::createOne(['name' => self::MENU_UPDATE, 'resolution' => "1920x1080"]);
        $iri = $this->findIriBy(Menu::class, ['name' => self::MENU_UPDATE]);

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

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

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

        MenuFactory::createOne(['name' => self::MENU_DELETE, 'resolution' => "1920x1080"]);
        $iri = $this->findIriBy(Menu::class, ['name' => self::MENU_DELETE]);

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