<?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)->getMock();

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

    /**
     * @see Utils::isLastParentAndCMF()
     */
    public function testIsLastParentAndCMF(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['isLastParentAndCMF'])
            ->getMock();

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->networkOrganizationRepository
                ->method('isLastParent')
                ->willReturnMap([
                    [$organization1, true],
                    [$organization2, true],
                    [$organization3, false],
                    [$organization4, false],
                ]);

        $this->networkUtils
                ->method('isCMF')
                ->willReturnMap([
                    [$organization1, true],
                    [$organization2, false],
                    [$organization3, true],
                    [$organization4, false],
                ]);

        $this->assertTrue($cotisationUtils->isLastParentAndCMF($organization1));
        $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization2));
        $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization3));
        $this->assertFalse($cotisationUtils->isLastParentAndCMF($organization4));
    }

    /**
     * @see Utils::isStructureAndCMF()
     */
    public function testIsStructureAndCMF(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['isStructureAndCMF'])
            ->getMock();

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->organizationUtils
            ->method('isStructure')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->with($organization1)
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($cotisationUtils->isStructureAndCMF($organization1));
        $this->assertFalse($cotisationUtils->isStructureAndCMF($organization2));
        $this->assertFalse($cotisationUtils->isStructureAndCMF($organization3));
        $this->assertFalse($cotisationUtils->isStructureAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndCMF()
     */
    public function testIsManagerAndCMF(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['isManagerAndCMF'])
            ->getMock();

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($cotisationUtils->isManagerAndCMF($organization1));
        $this->assertFalse($cotisationUtils->isManagerAndCMF($organization2));
        $this->assertFalse($cotisationUtils->isManagerAndCMF($organization3));
        $this->assertFalse($cotisationUtils->isManagerAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndNotLastParentAndCMF()
     */
    public function testIsManagerAndLastParentAndCMF(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['isManagerAndLastParentAndCMF'])
            ->getMock();

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $cotisationUtils
            ->method('isLastParentAndCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, false],
                [$organization4, true],
            ]);

        $this->assertTrue($cotisationUtils->isManagerAndLastParentAndCMF($organization1));
        $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization2));
        $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization3));
        $this->assertFalse($cotisationUtils->isManagerAndLastParentAndCMF($organization4));
    }

    /**
     * @see Utils::isManagerAndNotLastParentAndCMF()
     */
    public function testIsManagerAndNotLastParentAndCMF(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['isManagerAndNotLastParentAndCMF'])
            ->getMock();

        $organization1 = $this->getMockBuilder(Organization::class)->getMock();
        $organization2 = $this->getMockBuilder(Organization::class)->getMock();
        $organization3 = $this->getMockBuilder(Organization::class)->getMock();
        $organization4 = $this->getMockBuilder(Organization::class)->getMock();

        $this->organizationUtils
            ->method('isManager')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, false],
                [$organization3, true],
                [$organization4, true],
            ]);

        $this->networkOrganizationRepository
            ->method('isLastParent')
            ->willReturnMap([
                [$organization1, false],
                [$organization2, false],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->networkUtils
            ->method('isCMF')
            ->willReturnMap([
                [$organization1, true],
                [$organization2, true],
                [$organization3, true],
                [$organization4, false],
            ]);

        $this->assertTrue($cotisationUtils->isManagerAndNotLastParentAndCMF($organization1));
        $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization2));
        $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization3));
        $this->assertFalse($cotisationUtils->isManagerAndNotLastParentAndCMF($organization4));
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertState(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['getAlertState'])
            ->getMock();

        $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()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
        $this->assertEquals(AlertStateEnum::AFFILIATION()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
        $this->assertEquals(AlertStateEnum::INVOICE()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertStateInsurance(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['getAlertState'])
            ->getMock();

        $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()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
    }

    /**
     * @see Utils::getAlertState()
     */
    public function testGetAlertStateAdvertisingInsurance(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['getAlertState'])
            ->getMock();

        $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()->getValue(), $cotisationUtils->getAlertState($organization, $year) );
    }

    /**
     * @see Utils::getCurrentCotisationYear()
     */
    public function testGetCurrentCotisationYear(): void
    {
        $cotisationUtils = $this->getMockBuilder(CotisationUtils::class)
            ->setConstructorArgs([$this->networkUtils, $this->organizationUtils, $this->networkOrganizationRepository, $this->cotisationApiResourcesRepository])
            ->setMethodsExcept(['getCurrentCotisationYear'])
            ->getMock();

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

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

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

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