<?php

namespace App\Entity;

use App\Repository\CommandRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

#[ORM\Entity(repositoryClass: CommandRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_NAME', fields: ['name'])]
#[UniqueEntity(fields: ['name'], message: 'validators.command.name.unique')]
class Command extends AbstractEntity
{
    use NameableTrait;
    use ToggleableTrait;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $script = null;

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

    #[ORM\Column]
    private ?bool $readOnly = null;

    /**
     * @var Collection<int, CommandGroup>
     */
    #[ORM\ManyToMany(targetEntity: CommandGroup::class, mappedBy: 'commands')]
    private Collection $commandGroups;

    #[ORM\Column(nullable: true)]
    private ?bool $parameters = null;

    public function __construct()
    {
        parent::__construct();
        $this->commandGroups = new ArrayCollection();
    }

    public function getScript(): ?string
    {
        return $this->script;
    }

    public function setScript(string $script): static
    {
        $this->script = $script;

        return $this;
    }

    public function getComments(): ?string
    {
        return $this->comments;
    }

    public function setComments(?string $comments): static
    {
        $this->comments = $comments;

        return $this;
    }

    public function isReadOnly(): ?bool
    {
        return $this->readOnly;
    }

    public function setReadOnly(bool $readOnly): static
    {
        $this->readOnly = $readOnly;

        return $this;
    }

    /**
     * @return Collection<int, CommandGroup>
     */
    public function getCommandGroups(): Collection
    {
        return $this->commandGroups;
    }

    public function addCommandGroup(CommandGroup $commandGroup): static
    {
        if (!$this->commandGroups->contains($commandGroup)) {
            $this->commandGroups->add($commandGroup);
            $commandGroup->addCommand($this);
        }

        return $this;
    }

    public function removeCommandGroup(CommandGroup $commandGroup): static
    {
        if ($this->commandGroups->removeElement($commandGroup)) {
            $commandGroup->removeCommand($this);
        }

        return $this;
    }

    /**
     * @return Collection<int, CommandTask>
     */
    public function getCommandTasks(): Collection
    {
        return $this->commandTasks;
    }

    public function addCommandTask(CommandTask $commandTask): static
    {
        if (!$this->commandTasks->contains($commandTask)) {
            $this->commandTasks->add($commandTask);
            $commandTask->addCommand($this);
        }

        return $this;
    }

    public function removeCommandTask(CommandTask $commandTask): static
    {
        if ($this->commandTasks->removeElement($commandTask)) {
            $commandTask->removeCommand($this);
        }

        return $this;
    }

    public function isParameters(): ?bool
    {
        return $this->parameters;
    }

    public function setParameters(?bool $parameters): static
    {
        $this->parameters = $parameters;

        return $this;
    }
}
