<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\PersonBundle\Tests\Export\Export;

use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Service\RollingDate\RollingDate;
use Chill\MainBundle\Service\RollingDate\RollingDateConverterInterface;
use Chill\MainBundle\Test\Export\AbstractExportTest;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Export\Export\ListAccompanyingPeriod;
use Chill\PersonBundle\Export\Helper\FilterListAccompanyingPeriodHelper;
use Chill\PersonBundle\Export\Helper\ListAccompanyingPeriodHelper;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

/**
 * @internal
 *
 * @coversNothing
 */
class ListAccompanyingPeriodTest extends AbstractExportTest
{
    use ProphecyTrait;

    private readonly ListAccompanyingPeriod $listAccompanyingPeriod;

    private readonly CenterRepositoryInterface $centerRepository;

    protected function setUp(): void
    {
        parent::setUp();
        self::bootKernel();
    }

    public function getExport()
    {
        /** @var EntityManagerInterface::class $em */
        $em = self::getContainer()->get(EntityManagerInterface::class);
        $rollingDateConverter = self::getContainer()->get(RollingDateConverterInterface::class);
        $listAccompanyingPeriodHelper = self::getContainer()->get(ListAccompanyingPeriodHelper::class);
        $centerRepository = self::getContainer()->get(CenterRepositoryInterface::class);
        $scopeRepository = self::getContainer()->get(ScopeRepositoryInterface::class);

        // mock authorization helper
        $scopes = $scopeRepository->findAll();
        $scopesConfidentials = [] !== $scopes ? [$scopes[0]] : [];
        $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
        $authorizationHelper->getReachableScopes(Argument::type(User::class), AccompanyingPeriodVoter::SEE_DETAILS, Argument::type(Center::class))
            ->willReturn($scopes);
        $authorizationHelper->getReachableScopes(Argument::type(User::class), AccompanyingPeriodVoter::SEE_CONFIDENTIAL_ALL, Argument::type(Center::class))
            ->willReturn($scopesConfidentials);

        yield new ListAccompanyingPeriod(
            $em,
            $rollingDateConverter,
            $listAccompanyingPeriodHelper,
            new FilterListAccompanyingPeriodHelper(
                $centerRepository,
                $authorizationHelper->reveal(),
                $this->getParameters(true)
            )
        );

        yield new ListAccompanyingPeriod(
            $em,
            $rollingDateConverter,
            $listAccompanyingPeriodHelper,
            new FilterListAccompanyingPeriodHelper(
                $centerRepository,
                $authorizationHelper->reveal(),
                $this->getParameters(false)
            )
        );
    }

    public static function getFormData(): array
    {
        return [
            ['calc_date' => new RollingDate(RollingDate::T_TODAY)],
        ];
    }

    public static function getModifiersCombination(): array
    {
        return [[Declarations::ACP_TYPE]];
    }
}
