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


namespace App\Tests\Functional;

use App\Entity\DocumentObject;
use App\Factory\DocumentFactory;
use App\Factory\DocumentObjectFactory;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectProductFactory;
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 DocumentResourceTest 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 testCreateDocument(): void
    {
        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
            ]
        );
        $partner = PartnerFactory::createOne();
        $product = ProductFactory::createOne();
        $documentObject = DocumentObjectFactory::createOne();

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

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

        $this->browser()
            ->post('/api/documents', [
                'json' => [
                    'name' => 'my business file',
                    'description' => 'test',
                    'partner' => '/api/partners/' . $partner->getId(),
                    'product' => '/api/products/' . $product->getId(),
                    'documentObject' => '/api/document_objects/' . $documentObject->getId()
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertSuccessful()
            ;
    }

//    public function testDeleteDocument()
//    {
//        $user = UserFactory::createOne(
//            [
//                'firstName' => 'Peter',
//                'lastName' => 'Test',
//                'password' => 'test',
//                'email' => 'peter@test.de',
//                'roles' => ["ROLE_ADMIN"]
//            ]
//        );
//        $token = $this->JWTManager->create($user->object());
//        DocumentObjectFactory::createOne();
//        PartnerFactory::createOne();
//        ProductFactory::createOne();
//        $document = DocumentFactory::createOne();
//
//        $this->browser()
//            ->delete('/api/documents/' . $document->getId(), [
//                'headers' => [
//                    'Authorization' => 'Bearer ' . $token,
//                ],
//            ])
//            ->assertSuccessful()
//        ;
//    }
}
