<?php

namespace App\Tests\Unit\Service\Cotisation;

use App\Entity\Organization\Organization;
use App\Enum\Cotisation\AlertStateEnum;
use App\Repository\Cotisation\CotisationApiResourcesRepository;
use App\Repository\Network\NetworkOrganizationRepository;
use App\Service\Cotisation\Utils as CotisationUtils;
use App\Service\Network\Utils as NetworkUtils;
use App\Service\Organization\Utils as OrganizationUtils;
use App\Service\Utils\DatesUtils;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class UtilsTest extends TestCase
{
    private const MEMBERSHIP_WAITING = 495; // Affiliation in progress
    private const MEMBERSHIP_NOPAYMENT = 517; // Waiting paiement
    private const SUBMIT_IN_PROGRESS = 540; // Affiliation in progress

    private MockObject|NetworkOrganizationRepository $networkOrganizationRepository;
    private MockObject|NetworkUtils $networkUtils;
    private MockObject|OrganizationUtils $organizationUtils;
    private MockObject|CotisationApiResourcesRepository $cotisationApiResourcesRepository;

    public function setUp(): void
    {
        $this->networkOrganizationRepository = $this->getMockBuilder(NetworkOrganizationRepository::class)
            ->disableOriginalConstructor()
            ->getMock();

        $this->networkUtils = $this->getMockBuilder(NetworkUtils::class)->disableOriginalConstructor()->getMock();
        $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();

        $this->cotisationApiResourcesRepository = $this->getMockBuilder(CotisationApiResourcesRepository::class)
            ->disableOriginalConstructor()
            ->getMock();
    }

    public function getCotisationUtilsMockFor(string $methodName): MockObject|CotisationUtils
    {
        return $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept([$methodName])
            ->getMock();
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertState(): void
    {
        $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getId')->willReturn(1);

        $year = 2022;

        $this->cotisationApiResourcesRepository
            ->method('getAffiliationState')
            ->with($organization->getId(), $year)
            ->willReturnOnConsecutiveCalls(
                self::MEMBERSHIP_WAITING, self::SUBMIT_IN_PROGRESS, self::MEMBERSHIP_NOPAYMENT
            );

        $this->assertEquals(AlertStateEnum::AFFILIATION, $cotisationUtils->getAlertState($organization, $year));
        $this->assertEquals(AlertStateEnum::AFFILIATION, $cotisationUtils->getAlertState($organization, $year));
        $this->assertEquals(AlertStateEnum::INVOICE, $cotisationUtils->getAlertState($organization, $year));
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertStateInsurance(): void
    {
        $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');

        $year = 2022;

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getId')->willReturn(1);

        $this->cotisationApiResourcesRepository
            ->method('getAffiliationState')
            ->with($organization->getId(), $year)
            ->willReturn(null);

        $this->cotisationApiResourcesRepository
            ->method('isInsuranceNotDone')
            ->with($organization->getId(), $year)
            ->willReturn(true);

        $this->assertEquals(AlertStateEnum::INSURANCE, $cotisationUtils->getAlertState($organization, $year));
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertStateAdvertisingInsurance(): void
    {
        $cotisationUtils = $this->getCotisationUtilsMockFor('getAlertState');

        $year = 2022;

        $organization = $this->getMockBuilder(Organization::class)->getMock();
        $organization->method('getId')->willReturn(1);

        $this->cotisationApiResourcesRepository
            ->method('getAffiliationState')
            ->with($organization->getId(), $year)
            ->willReturn(null);

        $this->cotisationApiResourcesRepository
            ->method('isNotDGVCustomer')
            ->with($organization->getId())
            ->willReturn(true);

        $this->assertEquals(AlertStateEnum::ADVERTISINGINSURANCE, $cotisationUtils->getAlertState($organization, $year));
    }

    /**
     * @see Utils::getCurrentCotisationYear()
     */
    public function testGetCurrentCotisationYear(): void
    {
        $cotisationUtils = $this->getCotisationUtilsMockFor('getCurrentCotisationYear');

        DatesUtils::setFakeDatetime('2022-03-01');

        $this->assertEquals(
            2022,
            $cotisationUtils->getCurrentCotisationYear()
        );

        DatesUtils::setFakeDatetime('2022-10-01');

        $this->assertEquals(
            2023,
            $cotisationUtils->getCurrentCotisationYear()
        );
    }
}
