<?php

namespace App\Entity;

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

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

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

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

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

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

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

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

    #[ORM\Column]
    private ?\DateTime $date_create = null;

    #[ORM\Column(type: Types::SMALLINT)]
    private ?int $last_state = null;


    #[ORM\ManyToOne(inversedBy: 'investigations')]
    #[ORM\JoinColumn(nullable: false)]
    private ?User $create_by = null;

    /**
     * @var Collection<int, Question>
     */
    #[ORM\OneToMany(targetEntity: Question::class, mappedBy: 'investigation', orphanRemoval: true, cascade: ['persist', 'remove'])]
    private Collection $questions;

    /**
     * @var Collection<int, Broadcast>
     */
    #[ORM\OneToMany(targetEntity: Broadcast::class, mappedBy: 'investigation')]
    private Collection $broadcasts;

    /**
     * @var Collection<int, State>
     */
    #[ORM\OneToMany(targetEntity: State::class, mappedBy: 'investigation')]
    #[ORM\OrderBy(['date' => 'DESC'])]
    private Collection $states;


    public function __construct()
    {
        $this->questions = new ArrayCollection();
        $this->date_create = new \DateTime();
        $this->last_state = 11;
        $this->addQuestion(new Question());
        $this->broadcasts = new ArrayCollection();
        $this->states = new ArrayCollection();
    }

    public function __clone()
    {
        $this->name_en = $this->name_en ." duplicate";
        $this->name_fr = $this->name_fr ." duplicate";
        $this->name_ar = $this->name_ar ." duplicate";
        $this->date_create = new \DateTime();
        $this->last_state = 12;
        $this->broadcasts = new ArrayCollection();
    }

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

    public function getNameEn(): ?string
    {
        return $this->name_en;
    }

    public function setNameEn(?string $name_en): static
    {
        $this->name_en = $name_en;

        return $this;
    }

    public function getSourceEn(): ?string
    {
        return $this->source_en;
    }

    public function setSourceEn(?string $source_en): static
    {
        $this->source_en = $source_en;

        return $this;
    }

    public function getNameFr(): ?string
    {
        return $this->name_fr;
    }

    public function setNameFr(?string $name_fr): static
    {
        $this->name_fr = $name_fr;

        return $this;
    }

    public function getSourceFr(): ?string
    {
        return $this->source_fr;
    }

    public function setSourceFr(?string $source_fr): static
    {
        $this->source_fr = $source_fr;

        return $this;
    }

    public function getNameAr(): ?string
    {
        return $this->name_ar;
    }

    public function setNameAr(?string $name_ar): static
    {
        $this->name_ar = $name_ar;

        return $this;
    }

    public function getSourceAr(): ?string
    {
        return $this->source_ar;
    }

    public function setSourceAr(?string $source_ar): static
    {
        $this->source_ar = $source_ar;

        return $this;
    }

    public function getDateCreate(): ?\DateTime
    {
        return $this->date_create;
    }

    public function setDateCreate(\DateTime $date_create): static
    {
        $this->date_create = $date_create;

        return $this;
    }

    public function getLastState(): ?int
    {
        return $this->last_state;
    }

    public function setLastState(int $last_state): static
    {
        $this->last_state = $last_state;

        return $this;
    }

    public function getCreateBy(): ?User
    {
        return $this->create_by;
    }

    public function setCreateBy(?User $create_by): static
    {
        $this->create_by = $create_by;

        return $this;
    }

    /**
     * @return Collection<int, Question>
     */
    public function getQuestions(): Collection
    {
        return $this->questions;
    }

    public function addQuestion(Question $question): static
    {
        if (!$this->questions->contains($question)) {
            $this->questions->add($question);
            $question->setInvestigation($this);
        }

        return $this;
    }

    public function removeQuestion(Question $question): static
    {
        if ($this->questions->removeElement($question)) {
            // set the owning side to null (unless already changed)
            if ($question->getInvestigation() === $this) {
                $question->setInvestigation(null);
            }
        }

        return $this;
    }

    /**
     * @return int
     */
    public function getNbrQuestions(): int
    {
        return count($this->questions);
    }

    /**
     * @return Collection<int, Broadcast>
     */
    public function getBroadcasts(): Collection
    {
        return $this->broadcasts;
    }

    public function addBroadcast(Broadcast $broadcast): static
    {
        if (!$this->broadcasts->contains($broadcast)) {
            $this->broadcasts->add($broadcast);
            $broadcast->setInvestigation($this);
        }

        return $this;
    }

    public function removeBroadcast(Broadcast $broadcast): static
    {
        if ($this->broadcasts->removeElement($broadcast)) {
            // set the owning side to null (unless already changed)
            if ($broadcast->getInvestigation() === $this) {
                $broadcast->setInvestigation(null);
            }
        }

        return $this;
    }

    public function duplicateQuestions(): Collection
    {
        $dupQuestions = new ArrayCollection();
        foreach($this->questions as $question){
            $dupQuestions->add(clone $question);
        }
        return $dupQuestions;
    }

    /**
     * @return Collection<int, State>
     */
    public function getStates(): Collection
    {
        return $this->states;
    }

    public function addState(State $state): static
    {
        if (!$this->states->contains($state)) {
            $this->states->add($state);
            $state->setInvestigation($this);
        }

        return $this;
    }

    public function removeState(State $state): static
    {
        if ($this->states->removeElement($state)) {
            // set the owning side to null (unless already changed)
            if ($state->getInvestigation() === $this) {
                $state->setInvestigation(null);
            }
        }

        return $this;
    }

    public function getState(): ?State
    {
        return $this->states->first() ?: null;
    }
   
}
