<?php
declare(strict_types=1);

namespace App\Service\Organization;

use App\ApiResources\Profile\OrganizationProfile;
use App\Entity\Organization\Organization;
use App\Enum\Organization\PrincipalTypeEnum;
use App\Service\Network\Tree;
use App\Service\Security\Module;
use App\Service\Organization\Utils as OrganizationUtils;
use App\Test\Service\Organization\OrganizationProfileCreatorTest;

/**
 * Class OrganizationProfileCreator : Service contenant les manipulations associés à la ressource OrganizationProfile
 * @package App\Service\Organization
 */
class OrganizationProfileCreator
{
    public function __construct(
        private Module $module,
        private Tree $tree,
        private OrganizationUtils $organizationUtils
    )
    { }

    /**
     * Classe permettant de créer le profile d'une organisation à partir d'une entité Organization
     * @param Organization $organization
     * @return OrganizationProfile
     * @see OrganizationProfileCreatorTest::testCreateCompleteOrganizationProfile()
     */
    public function createCompleteOrganizationProfile(Organization $organization): OrganizationProfile
    {
        $organizationProfile = $this->createLightOrganizationProfile($organization);
        $organizationProfile->setModules($this->module->getOrganizationModules($organization));
        $organizationProfile->setProduct($organization->getSettings()->getProduct());
        $organizationProfile->setParametersId($organization->getParameters()->getId());
        $organizationProfile->setLegalStatus($organization->getLegalStatus());
        $organizationProfile->setHasChildren($organization->getNetworkOrganizationChildren()->count() > 1);
        $organizationProfile->setShowAdherentList(
            $organization->getParameters()->getShowAdherentList() &&
            $organization->getPrincipalType() !== PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue()
        );

        foreach ($organization->getNetworkOrganizations() as $networkOrganization){
            $organizationProfile->addNetwork($networkOrganization->getNetwork()->getName());
        }

        /** @var \App\Entity\Organization\Organization $parent */
        foreach ($this->tree->findAllParentsAndSortByType($organization) as $parent){
            $parentProfile = $this->createLightOrganizationProfile($parent);
            $organizationProfile->addParent($parentProfile);
        }

        $organizationProfile->setCurrentYear($this->organizationUtils->getOrganizationCurrentActivityYear($organization));

        return $organizationProfile;
    }

    /**
     * Classe permettant de créer et Setter les éléments de base d'un profile d'organisation à partir d'une entité Organization
     * @param Organization $organization
     * @return OrganizationProfile
     */
    public function createLightOrganizationProfile(Organization $organization): OrganizationProfile
    {
        $organizationProfile = new OrganizationProfile();
        $organizationProfile
            ->setId($organization->getId())
            ->setName($organization->getName())
            ->setWebsite($this->organizationUtils->getOrganizationWebsite($organization))
        ;

        return $organizationProfile;
    }
}
