<?php

namespace App\Entity;

use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $description = null;

    #[ORM\ManyToOne(targetEntity: MediaObject::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: "SET NULL")]
    private ?MediaObject $image = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?User $createdBy = null;

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

    #[ORM\OneToMany(mappedBy: 'product', targetEntity: Sale::class)]
    private Collection $sales;

    #[ORM\OneToMany(mappedBy: 'product', targetEntity: Document::class)]
    private Collection $documentObjects;

    #[ORM\OneToMany(mappedBy: 'product', targetEntity: Posting::class)]
    private Collection $postings;

    #[ORM\OneToMany(mappedBy: 'product', targetEntity: PartnerProduct::class)]
    private Collection $partnerProducts;

    public function __construct(User $createdBy)
    {
        $this->createdBy = $createdBy;
        $this->createdAt = new \DateTimeImmutable();
        $this->sales = new ArrayCollection();
        $this->documentObjects = new ArrayCollection();
        $this->postings = new ArrayCollection();
        $this->partnerProducts = new ArrayCollection();
    }

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(?string $description): static
    {
        $this->description = $description;

        return $this;
    }

    public function getImage(): ?MediaObject
    {
        return $this->image;
    }

    public function setImage(?MediaObject $image): static
    {
        $this->image = $image;

        return $this;
    }

    public function getCreatedBy(): ?User
    {
        return $this->createdBy;
    }

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

    /**
     * @return Collection<int, Sale>
     */
    public function getSales(): Collection
    {
        return $this->sales;
    }

    public function addSale(Sale $sale): static
    {
        if (!$this->sales->contains($sale)) {
            $this->sales->add($sale);
            $sale->setProduct($this);
        }

        return $this;
    }

    public function removeSale(Sale $sale): static
    {
        if ($this->sales->removeElement($sale)) {
            // set the owning side to null (unless already changed)
            if ($sale->getProduct() === $this) {
                $sale->setProduct(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Document>
     */
    public function getDocumentObjects(): Collection
    {
        return $this->documentObjects;
    }

    public function addDocumentObject(Document $documentObject): static
    {
        if (!$this->documentObjects->contains($documentObject)) {
            $this->documentObjects->add($documentObject);
            $documentObject->setProduct($this);
        }

        return $this;
    }

    public function removeDocumentObject(Document $documentObject): static
    {
        if ($this->documentObjects->removeElement($documentObject)) {
            // set the owning side to null (unless already changed)
            if ($documentObject->getProduct() === $this) {
                $documentObject->setProduct(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, Posting>
     */
    public function getPostings(): Collection
    {
        return $this->postings;
    }

    public function addPosting(Posting $posting): static
    {
        if (!$this->postings->contains($posting)) {
            $this->postings->add($posting);
            $posting->setProduct($this);
        }

        return $this;
    }

    public function removePosting(Posting $posting): static
    {
        if ($this->postings->removeElement($posting)) {
            // set the owning side to null (unless already changed)
            if ($posting->getProduct() === $this) {
                $posting->setProduct(null);
            }
        }

        return $this;
    }

    /**
     * @return Collection<int, PartnerProduct>
     */
    public function getPartnerProducts(): Collection
    {
        return $this->partnerProducts;
    }

    public function addPartnerProduct(PartnerProduct $partnerProduct): static
    {
        if (!$this->partnerProducts->contains($partnerProduct)) {
            $this->partnerProducts->add($partnerProduct);
            $partnerProduct->setProduct($this);
        }

        return $this;
    }

    public function removePartnerProduct(PartnerProduct $partnerProduct): static
    {
        if ($this->partnerProducts->removeElement($partnerProduct)) {
            // set the owning side to null (unless already changed)
            if ($partnerProduct->getProduct() === $this) {
                $partnerProduct->setProduct(null);
            }
        }

        return $this;
    }
}
