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


namespace App\Tests\Functional;

use App\Enum\PartnerType;
use App\Factory\CommentFactory;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectContactFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\PostingFactory;
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 CommentResourceTest 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 testPostComment(): void
    {
        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
            ]
        );
        ProductFactory::createOne();
        PartnerFactory::createOne();
        SaleFactory::createOne();
        $posting = PostingFactory::createOne();
        CommentFactory::createOne();

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

        $this->browser()
            ->get('/api/comments', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
            ->assertJsonMatches('"hydra:totalItems"', 1)
            //->assertJsonMatches('"hydra:member"[0].message', 'my comment')
        ;


        $this->browser()
            ->post('/api/comments' , [
                'json' => [
                    'message' => 'my comment',
                    'post' => '/api/posts/' . $posting->getId(),
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertSuccessful()
        ;

        $this->browser()
            ->get('/api/comments', [
                    'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ],
            ])
            ->assertSuccessful()
            ->assertJsonMatches('"hydra:totalItems"', 2)
            ->assertJsonMatches('"hydra:member"[1].message', 'my comment')
        ;

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