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


namespace App\Tests\Functional;

use App\Factory\DocumentFactory;
use App\Factory\DocumentObjectFactory;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\ProductFactory;
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 DocumentObjectResourceTest 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 testCreateDocumentObject(): 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',
            ]
        );

        $documentObject = DocumentObjectFactory::createOne();
        PartnerFactory::createOne();
        ProductFactory::createOne();
        $document = DocumentFactory::createOne();

        $token = $this->JWTManager->create($user->object());

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

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

        $this->browser()
            ->patch('/api/documents/' . $document->getId(),
                [
                'json' => [
                    'documentObject' => '/api/document_objects/' . $documentObject->getId()
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/merge-patch+json'
                ],
            ])
            ->assertSuccessful()
        ;

        $this->browser()
            ->delete('/api/document_objects/' . $documentObject->getId(),
                [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertStatus(500)
        ;
    }

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