<?php
declare(strict_types=1);

namespace App\Service\OnChange\Organization;

use App\Entity\Organization\Organization;
use App\Enum\Organization\LegalEnum;
use App\Service\OnChange\OnChangeContext;
use App\Service\OnChange\OnChangeDefault;
use App\Test\Service\OnChange\Organization\OnOrganizationChangeTest;

/**
 * Classe OnOrganizationChange qui comporte toutes les opérations automatiquent se produisant lors de l'évolution d'une organisation
 */
class OnOrganizationChange extends OnChangeDefault
{
    public function beforeChange(mixed $organization, OnChangeContext $context): void
    {
        if($context->previousData() && $context->previousData()->getLegalStatus() !== $organization->getLegalStatus()) {
            $this->onLegalStatusChange($organization);
        }
    }

    /**
     * Si le statut de l'organization évolue, on doit faire évoluer d'autre paramètres.
     * @param Organization $organization
     * @see OnOrganizationChangeTest::testOnLegalStatusChange()
     */
    public function onLegalStatusChange(Organization $organization): void{
        if($organization->getLegalStatus() !== LegalEnum::ASSOCIATION_LAW_1901()->getValue()){
            $organization->getParameters()->setShowAdherentList(false);
        }

        if($organization->getLegalStatus() === LegalEnum::COMMERCIAL_SOCIETY()->getValue()){
            $organization->getBillingSetting()->setApplyVat(true);
        }
    }
}
