<?php

namespace App\Entity;

use App\Repository\PartnerProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: PartnerProductRepository::class)]
#[ORM\UniqueConstraint(name: "unique_partner_product", columns: ["partner_id", "product_id"])]
class PartnerProduct
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(inversedBy: 'partnerProducts')]
    #[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
    private ?Partner $partner = null;

    #[ORM\ManyToOne(inversedBy: 'partnerProducts')]
    #[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
    private ?Product $product = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\OneToMany(mappedBy: 'partnerProduct', targetEntity: ContactPartnerProduct::class)]
    private Collection $contactPartnerProducts;

    public function __construct(Partner $partner, Product $product)
    {
        $this->partner = $partner;
        $this->product = $product;
        $this->createdAt = new \DateTimeImmutable();
        $this->contactPartnerProducts = new ArrayCollection();
    }

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

    public function getPartner(): ?Partner
    {
        return $this->partner;
    }

    public function setPartner(?Partner $partner): static
    {
        $this->partner = $partner;

        return $this;
    }

    public function getProduct(): ?Product
    {
        return $this->product;
    }

    public function setProduct(?Product $product): static
    {
        $this->product = $product;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(?\DateTimeImmutable $createdAt): void
    {
        $this->createdAt = $createdAt;
    }

    /**
     * @return Collection<int, ContactPartnerProduct>
     */
    public function getContactPartnerProducts(): Collection
    {
        return $this->contactPartnerProducts;
    }

    public function addContactPartnerProduct(ContactPartnerProduct $contactPartnerProduct): static
    {
        if (!$this->contactPartnerProducts->contains($contactPartnerProduct)) {
            $this->contactPartnerProducts->add($contactPartnerProduct);
            $contactPartnerProduct->setPartnerProduct($this);
        }

        return $this;
    }

    public function removeContactPartnerProduct(ContactPartnerProduct $contactPartnerProduct): static
    {
        if ($this->contactPartnerProducts->removeElement($contactPartnerProduct)) {
            // set the owning side to null (unless already changed)
            if ($contactPartnerProduct->getPartnerProduct() === $this) {
                $contactPartnerProduct->setPartnerProduct(null);
            }
        }

        return $this;
    }

}
