<?php

namespace App\Tests\Fixture;

use App\Entity\Booking\Event;
use App\Entity\Core\ContactPoint;
use App\Entity\Network\Network;
use App\Entity\Network\NetworkOrganization;
use App\Entity\Organization\Organization;
use App\Entity\Organization\Parameters;
use App\Enum\Booking\VisibilityEnum;
use App\Enum\Core\ContactPointTypeEnum;
use App\Enum\Organization\LegalEnum;
use App\Enum\Organization\PrincipalTypeEnum;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Serializer\Context\SerializerContextBuilder;
use App\Service\Utils\Uuid;

class OrganizationFixtures extends Fixture
{
    public function load(ObjectManager $em): void
    {
        $network = new Network();
        $network->setName('NET');
        $em->persist($network);

        // Create the root organization (opentalent)
        $parameters0 = new Parameters();
        $em->persist($parameters0);

        $contactPoint0 = new ContactPoint();
        $contactPoint0->setContactType(ContactPointTypeEnum::PRINCIPAL()->getValue());
        $em->persist($contactPoint0);

        $root = new Organization();
        $root->setName('Root');
        $root->setLegalStatus(LegalEnum::COMMERCIAL_SOCIETY()->getValue());
        $root->setPrincipalType(PrincipalTypeEnum::NATIONAL_FEDERATION()->getValue());
        $root->setParameters($parameters0);
        $root->addContactPoint($contactPoint0);
        $em->persist($root);

        $networkOrganisation0 = new NetworkOrganization();
        $networkOrganisation0->setOrganization($root);
        $networkOrganisation0->setNetwork($network);
        $em->persist($networkOrganisation0);


        // Create a federation organization
        $parameters1 = new Parameters();
        $em->persist($parameters1);

        $contactPoint1 = new ContactPoint();
        $contactPoint1->setContactType(ContactPointTypeEnum::PRINCIPAL()->getValue());
        $em->persist($contactPoint1);

        $fede = new Organization();
        $fede->setName('Fede 1');
        $fede->setLegalStatus(LegalEnum::ASSOCIATION_LAW_1901()->getValue());
        $fede->setPrincipalType(PrincipalTypeEnum::NATIONAL_FEDERATION()->getValue());
        $fede->setParameters($parameters1);
        $fede->addContactPoint($contactPoint1);
        $em->persist($fede);

        $networkOrganisation1 = new NetworkOrganization();
        $networkOrganisation1->setOrganization($fede);
        $networkOrganisation1->setNetwork($network);
        $networkOrganisation1->setParent($root);
        $em->persist($networkOrganisation1);

        // Create a simple organization
        $parameters2 = new Parameters();
        $em->persist($parameters2);

        $contactPoint2 = new ContactPoint();
        $contactPoint2->setContactType(ContactPointTypeEnum::PRINCIPAL()->getValue());
        $em->persist($contactPoint2);

        $organization = new Organization();
        $organization->setName('Organization 2');
        $organization->setLegalStatus(LegalEnum::ASSOCIATION_LAW_1901()->getValue());
        $organization->setPrincipalType(PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue());
        $organization->setParameters($parameters2);
        $organization->addContactPoint($contactPoint2);
        $em->persist($organization);

        $networkOrganisation2 = new NetworkOrganization();
        $networkOrganisation2->setOrganization($organization);
        $networkOrganisation2->setNetwork($network);
        $networkOrganisation2->setParent($fede);
        $em->persist($networkOrganisation2);


        // Booking
        $now = new \DateTime('now');
        $start = $now->add(new \DateInterval('P1M'));
        $end = $now->add(new \DateInterval('P1M1D'));

        $event = new Event();
        $event->setOrganization($organization);
        $event->setName('My Event');
        $event->setDatetimeStart($start);
        $event->setDatetimeEnd($end);
        $event->setVisibility(VisibilityEnum::PUBLIC_VISIBILITY());
        $event->setUuid(Uuid::uuid());
        $em->persist($event);

        $em->flush();
    }
}
