<?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\ContactFactory;
use App\Factory\MediaObjectLogoFactory;
use App\Factory\MediaObjectContactFactory;
use App\Factory\MediaObjectUserFactory;
use App\Factory\PartnerFactory;
use App\Factory\PostingFactory;
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 TaskResourceTest 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 testPostTask(): void
    {
        $user = UserFactory::createOne(
            [
                'firstName' => 'Peter',
                'lastName' => 'Test',
                'password' => 'test',
                'email' => 'peter@test.de',
            ]
        );
        $partner = PartnerFactory::createOne();
        $contact = ContactFactory::createOne();

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

        $this->browser()
            ->post('/api/tasks' , [
                'json' => [
                      'headline' => 'new task',
                      'description' => 'this is what is to do...',
                      'assignedTo' => '/api/users/' . $user->getId(),
                      'dueAt'=> "2024-03-12T08:57:39.892Z",
                      'partner'=> "/api/partners/" . $partner->getId(),
                      'contact'=> "/api/contacts/" . $contact->getId(),
                      'prio'=> "low",
                      'completed'=> false,
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $token,
                ]
            ])
            ->assertSuccessful()
        ;

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

    }
}