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


namespace App\Tests\Functional;

use App\Enum\PartnerType;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\ProductFactory;
use App\Factory\SaleFactory;
use App\Factory\UserFactory;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

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

    private JWTTokenManagerInterface $JWTManager;

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

    public function testPostPartner(): void
    {
        $user = UserFactory::createOne(
            [
                'email' => 'peter@test.de',
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'roles' => ["ROLE_ADMIN"]
            ]
        );

        $mediaObject = MediaObjectLogoFactory::createOne();
        PartnerFactory::createOne();
        ProductFactory::createOne();
        SaleFactory::createOne();

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

        $response = $this->browser()
            ->post('/api/partners' , [
                'json' => [
                    'name' => 'test customer',
                    'partnerType' => PartnerType::Customer,
                    'street' => 'test street',
                    'streetNo' => '11',
                    'zip' => '22335',
                    'city' => 'test city',
                    'country' => 'test country',
                    'website' => 'wwe.test.de',
                    'logo' => '/api/media_objects/' . $mediaObject->getId(),
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                    'Content-Type' => 'application/ld+json'
                ]
            ])
            ->assertSuccessful()
            ->content()
        ;

        $this->browser()
            ->get('/api/partners', [
                    'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
            ->assertJsonMatches('"hydra:totalItems"', 2)
            ->content()
        ;

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