<?php

namespace App\Entity;

use App\Enum\TaskSchemaStatus;
use App\Enum\TaskStatus;
use App\Repository\TaskSchemaRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ORM\Entity(repositoryClass: TaskSchemaRepository::class)]
class TaskSchema
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    #[Groups(['task_schema:read'])]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups(['task_schema:read'])]
    private string $name = '';

    #[ORM\Column(enumType: TaskSchemaStatus::class)]
    #[Groups(['task_schema:read'])]
    private TaskSchemaStatus $status = TaskSchemaStatus::Active;

    #[ORM\Column(enumType: TaskStatus::class)]
    #[Groups(['task_schema:read'])]
    private TaskStatus $taskStatus = TaskStatus::Active;

    #[ORM\Column(type: 'date_immutable', nullable: true)]
    #[Groups(['task_schema:read'])]
    private ?\DateTimeImmutable $date = null;

    #[ORM\Column(name: '`repeat`', type: 'json', nullable: true)]
    #[Groups(['task_schema:read'])]
    private ?array $repeat = null;

    #[ORM\Column(name: '`start`', type: 'date_immutable', nullable: true)]
    #[Groups(['task_schema:read'])]
    private ?\DateTimeImmutable $start = null;

    #[ORM\Column(name: '`end`', type: 'date_immutable', nullable: true)]
    #[Groups(['task_schema:read'])]
    private ?\DateTimeImmutable $end = null;

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

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

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

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

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

        return $this;
    }

    public function getStatus(): TaskSchemaStatus
    {
        return $this->status;
    }

    public function setStatus(TaskSchemaStatus $status): self
    {
        $this->status = $status;

        return $this;
    }

    public function getTaskStatus(): TaskStatus
    {
        return $this->taskStatus;
    }

    public function setTaskStatus(TaskStatus $taskStatus): self
    {
        $this->taskStatus = $taskStatus;

        return $this;
    }

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

    public function setDate(?\DateTimeImmutable $date): self
    {
        $this->date = $date;

        return $this;
    }

    public function getRepeat(): ?array
    {
        return $this->repeat;
    }

    public function getRepeatType(): ?string
    {
        return $this->repeat !== null ? array_key_first($this->repeat) : null;
    }

    public function setRepeat(?array $repeat): self
    {
        $this->repeat = $repeat;

        return $this;
    }

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

    public function setStart(?\DateTimeImmutable $start): self
    {
        $this->start = $start;

        return $this;
    }

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

    public function setEnd(?\DateTimeImmutable $end): self
    {
        $this->end = $end;

        return $this;
    }

    /** @return Collection<int, Task> */
    public function getTasks(): Collection
    {
        return $this->tasks;
    }
}
