<?php
declare (strict_types=1);

namespace App\Entity\Network;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use App\Attribute\DateTimeConstraintAware;
use App\Entity\Organization\Organization;
use App\Repository\Network\NetworkOrganizationRepository;

//use DH\Auditor\Provider\Doctrine\Auditing\Annotation\Auditable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Fait le lien entre une Organization et un Network
 */
#[ApiResource(
    operations: [
        new Get(
            security: 'is_granted("ROLE_ORGANIZATION_VIEW" and object.getOrganization().getId() == user.getOrganization().getId()'
        ),
        new GetCollection(
            security: 'is_granted(\'ROLE_ORGANIZATION_VIEW\')'
        )
    ],
    normalizationContext: ['groups' => ['network']
    ],
    security: 'is_granted(\'ROLE_ORGANIZATION\')'
)]
//#[Auditable]
#[ORM\Entity(repositoryClass: NetworkOrganizationRepository::class)]
#[DateTimeConstraintAware(startDateFieldName: "startDate", endDateFieldName: "endDate")]
class NetworkOrganization
{
    #[ORM\Id]
    #[ORM\Column]
    #[ORM\GeneratedValue]
    #[Groups("network")]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'organizations')]
    #[ORM\JoinColumn(nullable: true)]
    #[Groups("network")]
    private Network $network;

    #[ORM\ManyToOne(inversedBy: 'networkOrganizations')]
    #[ORM\JoinColumn(nullable: true)]
    private Organization $organization;

    #[ORM\ManyToOne(inversedBy: 'networkOrganizationChildren')]
    private ?Organization $parent;

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $leadingCause = null;

    #[ORM\Column(type: 'date', nullable: true)]
    #[Groups("network")]
    private ?\DateTimeInterface $startDate = null;

    #[ORM\Column(type: 'date', nullable: true)]
    private ?\DateTimeInterface $endDate = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getNetwork(): ?Network
    {
        return $this->network;
    }

    public function setNetwork(?Network $network): self
    {
        $this->network = $network;
        return $this;
    }

    public function getOrganization(): ?Organization
    {
        return $this->organization;
    }

    public function setOrganization(?Organization $organization): self
    {
        $this->organization = $organization;
        return $this;
    }

    public function getParent(): ?Organization
    {
        return $this->parent;
    }

    public function setParent(?Organization $parent): self
    {
        $this->parent = $parent;
        return $this;
    }

    public function getLeadingCause(): ?string
    {
        return $this->leadingCause;
    }

    public function setLeadingCause(?string $leadingCause): self
    {
        $this->leadingCause = $leadingCause;
        return $this;
    }

    public function getStartDate(): ?\DateTimeInterface
    {
        return $this->startDate;
    }

    public function setStartDate(?\DateTime $startDate = null): self
    {
        if ($startDate == null) {
            $startDate = new \DateTime();
        }
        $this->startDate = $startDate;
        return $this;
    }

    public function getEndDate(): ?\DateTimeInterface
    {
        return $this->endDate;
    }

    public function setEndDate(?\DateTime $endDate = null): self
    {
        $this->endDate = $endDate;
        return $this;
    }
}
