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


namespace App\Tests\Functional;

use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectContactFactory;
use App\Factory\PartnerFactory;
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 ContactResourceTest 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 testPostContact(): void
    {

        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
            ]
        );
        $partner = PartnerFactory::createOne();

        $mediaObject = MediaObjectContactFactory::createOne();

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

        $this->browser()
            ->post('/api/contacts' , [
                'json' => [
                    'firstName' => 'Peter',
                    'lastName' => 'Test',
                    'partner' => '/api/partners/' . $partner->getId(),
                    'birthday' => '1984-02-10T00:00:00.000Z',
                    'image' => '/api/media_objects/' . $mediaObject->getId(),
                    'position' => 'CEO',
                    'phone' => '123456789',
                    'email' => 'peter@test2.de',
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertSuccessful()
        ;

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

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