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


namespace App\Tests\Functional;

use App\Factory\MediaObjectUserFactory;
use App\Factory\UserFactory;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Browser\Json;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class UserResourceTest 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 testPostUser(): void
    {
        UserFactory::createOne(
            [
                'email' => 'peter@test.de',
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'roles' => ["ROLE_ADMIN"]
            ]
        );
        $response = $this->browser()
            ->post('/auth', [
                'json' => [
                    'email' => 'peter@test.de',
                    'password' => 'test',
                ]
            ])
            ->use(function (Json $json) {
                $json->assertHas('token');
                $json->assertHas('id');
                $json->assertHas('email');
                $json->assertHas('firstName');
                $json->assertHas('lastName');
                $json->assertHas('roles');
            })
            ->assertSuccessful()
            ->content();
        $authResponseData = json_decode($response, true);
        $token = $authResponseData['token'];

        $this->browser()
            ->post('/api/users' , [
                'json' => [
                    'email' => 'norbert@test.de',
                    'firstName' => 'Norbert',
                    'lastName' => 'Test',
                    'password' => 'sehr geheim',
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertSuccessful()
        ;

        $this->browser()
            ->post('/auth', [
                'json' => [
                    'email' => 'norbert@test.de',
                    'password' => 'sehr geheim',
                ]
            ])
        ->assertSuccessful()
        ->assertJsonMatches('email', 'norbert@test.de');
    }

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

        $token = $this->JWTManager->create($user->object());
        $this->browser()
            ->post('/api/users' , [
                'json' => [
                    'email' => 'norbert@test.de',
                    'firstName' => 'Norbert',
                    'lastName' => 'Test',
                    'password' => 'sehr geheim',
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertStatus(403);
        ;
    }
}