<?php

namespace App\Entity;

use App\Entity\Traits\TimestampableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity()
 * @ORM\EntityListeners({"App\EntityListener\NegotiationMessageListener"})
 */
class NegotiationMessage implements BroadcastableInterface
{
    use TimestampableTrait;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Prospect", inversedBy="negotiationMessages", fetch="EAGER")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank()
     */
    private $prospect;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="negotiationMessages", fetch="EAGER")
     * @ORM\JoinColumn(nullable=false)
     * @Assert\NotBlank()
     * @Gedmo\Blameable(on="create")
     */
    private $user;

    /**
     * @ORM\Column(type="text")
     * @Assert\NotBlank()
     */
    private $description;

    /**
     * @ORM\Column(type="EnumMessageDirectionType")
     * @Assert\NotBlank()
     */
    private $direction;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Negotiation", inversedBy="messages", fetch="EAGER")
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     * @Assert\NotBlank()
     */
    private $negotiation;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $readAt;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\MailgunEvent", mappedBy="negotiationMessage", fetch="EAGER")
     */
    private $mailgunEvents;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\FileAssociationNegotiationMessage", fetch="EAGER",
     *     mappedBy="negotiationMessage", orphanRemoval=true, cascade={"persist"})
     */
    private $attachments;

    /**
     * @ORM\Column(type="EnumNegotiationMessageAttachContentType", options={"default": "NONE"}, nullable=true)
     */
    private $attachContentFormat;

    public function __construct()
    {
        $this->mailgunEvents = new ArrayCollection();
        $this->attachments = new ArrayCollection();
    }

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

    public function getProspect(): ?Prospect
    {
        return $this->prospect;
    }

    public function setProspect(?Prospect $prospect): self
    {
        $this->prospect = $prospect;

        return $this;
    }

    public function getUser(): ?User
    {
        return $this->user;
    }

    public function setUser(?User $user): self
    {
        $this->user = $user;

        return $this;
    }

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

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

        return $this;
    }

    public function getDirection()
    {
        return $this->direction;
    }

    public function setDirection(string $direction): self
    {
        $this->direction = $direction;

        return $this;
    }

    public function getNegotiation(): ?Negotiation
    {
        return $this->negotiation;
    }

    public function setNegotiation(?Negotiation $negotiation): self
    {
        $this->negotiation = $negotiation;

        return $this;
    }

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

    public function setReadAt(?\DateTimeInterface $readAt): self
    {
        $this->readAt = $readAt;

        return $this;
    }

    /**
     * @return Collection
     */
    public function getMailgunEvents(): Collection
    {
        return $this->mailgunEvents;
    }

    public function addMailgunEvent(MailgunEvent $mailgunEvent): self
    {
        if (!$this->mailgunEvents->contains($mailgunEvent)) {
            $this->mailgunEvents[] = $mailgunEvent;
            $mailgunEvent->setNegotiationMessage($this);
        }

        return $this;
    }

    public function removeMailgunEvent(MailgunEvent $mailgunEvent): self
    {
        if ($this->mailgunEvents->contains($mailgunEvent)) {
            $this->mailgunEvents->removeElement($mailgunEvent);
            // set the owning side to null (unless already changed)
            if ($mailgunEvent->getNegotiationMessage() === $this) {
                $mailgunEvent->setNegotiationMessage(null);
            }
        }

        return $this;
    }

    public function getDataType(): string
    {
        return 'negotiationmessage';
    }

    /**
     * @return Collection
     */
    public function getAttachments(): Collection
    {
        return $this->attachments;
    }

    public function addAttachment(FileAssociationNegotiationMessage $attachment): self
    {
        if (!$this->attachments->contains($attachment)) {
            $this->attachments[] = $attachment;
            $attachment->setNegotiationMessage($this);
        }

        return $this;
    }

    public function removeAttachment(FileAssociationNegotiationMessage $attachment): self
    {
        if ($this->attachments->contains($attachment)) {
            $this->attachments->removeElement($attachment);
            // set the owning side to null (unless already changed)
            if ($attachment->getNegotiationMessage() === $this) {
                $attachment->setNegotiationMessage(null);
            }
        }

        return $this;
    }

    public function getAttachContentFormat()
    {
        return $this->attachContentFormat;
    }

    public function setAttachContentFormat($attachContentFormat): self
    {
        $this->attachContentFormat = $attachContentFormat;

        return $this;
    }
}
