<?php

namespace Functional;

use App\Entity\Client;
use App\Entity\Image;
use App\Entity\ImageRepository;
use App\Entity\OrganizationalUnit;
use App\Entity\SoftwareProfile;
use App\Factory\ClientFactory;
use App\Factory\ImageFactory;
use App\Factory\ImageImageRepositoryFactory;
use App\Factory\ImageRepositoryFactory;
use App\Factory\OrganizationalUnitFactory;
use App\Factory\SoftwareProfileFactory;
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 ImageTest extends AbstractTest
{
    CONST string USER_ADMIN = 'ogadmin';
    CONST string IMAGE_CREATE = 'test-image-create';
    CONST string IMAGE_UPDATE = 'test-image-update';
    CONST string IMAGE_DELETE = 'test-image-delete';
    const string CLIENT = 'test-client';
    const string SOFTWARE_PROFILE = 'test-software-profile';

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

        ImageFactory::createMany(10);

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


    /*

    public function testCreateImage(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);

        SoftwareProfileFactory::createOne(['description' => self::SOFTWARE_PROFILE]);
        $swPIri = $this->findIriBy(SoftwareProfile::class, ['description' => self::SOFTWARE_PROFILE]);

        $imageRepositories = ImageImageRepositoryFactory::createMany(5);

        $this->createClientWithCredentials()->request('POST', '/images',['json' => [
            'name' => self::IMAGE_CREATE,
            'softwareProfile' => $swPIri,
            'imageImageRepositories' => array_map(fn($repo) => '/image-repositories/'. $repo->getUuid(), $imageRepositories)
        ]]);

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



    public function testUpdateImage(): void
    {
        UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]);

        ImageFactory::createOne(['name' => self::IMAGE_CREATE]);
        $iri = $this->findIriBy(Image::class, ['name' => self::IMAGE_CREATE]);

        $imageRepositories = ImageImageRepositoryFactory::createMany(5);

        $this->createClientWithCredentials()->request('PUT', $iri, ['json' => [
            'name' => self::IMAGE_UPDATE,
            'imageImageRepositories' => array_map(fn($repo) => '/image-repositories/'. $repo->getUuid(), $imageRepositories)
        ]]);

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

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

        ImageFactory::createOne(['name' => self::IMAGE_DELETE]);
        $iri = $this->findIriBy(Image::class, ['name' => self::IMAGE_DELETE]);

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