<?php

namespace App\Entity;

use App\Enum\TaskStatus;
use App\Repository\TaskRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Attribute\Ignore;

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

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

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

    #[ORM\Column(enumType: TaskStatus::class)]
    private TaskStatus $status = TaskStatus::Active;

    #[ORM\ManyToOne(targetEntity: TaskSchema::class, inversedBy: 'tasks')]
    #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
    #[Ignore]
    private ?TaskSchema $schema = null;

    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 getDate(): ?\DateTimeImmutable
    {
        return $this->date;
    }

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

        return $this;
    }

    #[Groups(['task:read'])]
    public function getStatus(): TaskStatus
    {
        if ($this->date !== null && $this->date < new \DateTimeImmutable('today')) {
            return TaskStatus::Past;
        }

        return $this->status;
    }

    public function getRawStatus(): TaskStatus
    {
        return $this->status;
    }

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

        return $this;
    }

    public function getSchema(): ?TaskSchema
    {
        return $this->schema;
    }

    public function setSchema(?TaskSchema $schema): self
    {
        $this->schema = $schema;

        return $this;
    }
}
