<?php declare(strict_types=1);

namespace Apexl\Tests\User\FeatureTests;

use apexl\Io\modules\user\entities\userEntity;
use apexl\Io\modules\user\exceptions\SessionException;
use apexl\Io\modules\user\interfaces\SessionManagerInterface;
use apexl\Io\modules\user\services\CurrentUserFactory;
use apexl\Io\modules\user\services\SessionMasqueradeService;
use apexl\Io\services\GlobalData;
use apexl\Io\TestInfrastructure\FeatureTest;
use DI\DependencyException;
use DI\NotFoundException;
use PHPUnit\Framework\Attributes\Test;
use function apexl\Io\container;

final class SessionMasqueradeTest extends FeatureTest
{
    /**
     * @throws SessionException
     * @throws NotFoundException
     * @throws DependencyException
     */
    #[Test]
    public function getCurrentUserWhenMasqueraded(): void
    {
        [$masquerader, $masqueradee] = userEntity::factory()->createMany(2);

        $globalData = container()->make(GlobalData::class);

        $sessionManager = container()->make(SessionManagerInterface::class, [
            'globalData' => $globalData
        ]);
        $session = $sessionManager->create($masquerader->id, 'test');

        $globalData->addData('entities', data: ['user' => ['entity' => $masquerader]]);
        $globalData->addData('session', $session);

        $masqueradeService = container()->make(SessionMasqueradeService::class, [
            'sessionManager' => $sessionManager,
        ]);

        $currentUserFactory = container()->make(CurrentUserFactory::class, [
            'masqueradeService' => $masqueradeService,
            'globalData' => $globalData,
        ]);

        $this->assertSame($masquerader->id, $currentUserFactory->get()->id);

        $masqueradeService->startMasquerade($masqueradee);

        $this->assertSame($masqueradee->id, $currentUserFactory->get()->id);
    }

}
