<?php
/**
 * @author Daniel Knudsen <d.knudsen@spawntree.de>
 * @date 01.03.24
 */


namespace App\Tests\Functional;

use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\UserFactory;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class MediaObjectResourceTest extends KernelTestCase
{
    use HasBrowser;
    use ResetDatabase;
    use Factories;

    private JWTTokenManagerInterface $JWTManager;
    private string $projectDir;

    protected function setUp(): void
    {
        parent::setUp();
        $this->JWTManager = self::getContainer()->get('lexik_jwt_authentication.jwt_manager');
        $this->projectDir = self::getContainer()->get('kernel')->getProjectDir();
    }

    public function testCreateMediaObject(): void
    {
        $path = $this->projectDir . '/tests/fixtures/';
        $srcFile = $path . '1176.png';
        $dstFile = $path . '1176_upload.png';
        copy($srcFile, $dstFile);

        $file = new UploadedFile($dstFile, 'image.png');

        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
                'roles' => ["ROLE_ADMIN"]
            ]
        );
        $token = $this->JWTManager->create($user->object());

        $this->browser()
            ->get('/api/media_objects', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
        ;

        $this->browser()
            ->post('/api/media_objects', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'multipart/form-data'
                ],
                'files' => [
                    'file' => $file,
                ],
            ])
            ->assertSuccessful()
            ;

        $partner = PartnerFactory::createOne();
        $this->browser()
            ->patch('/api/partners/' . $partner->getId(),
                [
                'json' => [
                    'logo' => '/api/media_objects/1'
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/merge-patch+json'
                ],
            ])
            ->assertSuccessful()
        ;

        $this->browser()
            ->delete('/api/media_objects/1',
                [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
        ;
    }

    public function testDeleteMediaObject()
    {
        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
            ]
        );
        $mediaObject = MediaObjectLogoFactory::createOne();
        $token = $this->JWTManager->create($user->object());
        $this->browser()
            ->delete('/api/media_objects/' . $mediaObject->getId(), [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
        ;
    }
}
