<?php

namespace App\Entity;

use App\Repository\ProjectRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

// Les projets sont des ensembles de tâches avec des paramètres de
// configuration pour la télécommande JOSM.
#[ORM\Entity(repositoryClass: ProjectRepository::class)]
class Project
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $description = null;

    #[ORM\Column(length: 255, unique: true)]
    #[Gedmo\Slug(fields: ['name'])]
    private ?string $slug = null;

    /**
     * @var Collection<int, Tag>
     */
    #[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'projects')]
    private Collection $tags;

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

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

    #[ORM\Column]
    #[Gedmo\Timestampable(on: 'create')]
    private ?\DateTimeImmutable $createdAt = null;

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

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

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

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $overpassQuery = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $overpassResult = null;

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

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

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

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

        return $this;
    }

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

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

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    /**
     * @return Collection<int, Tag>
     */
    public function getTags(): Collection
    {
        return $this->tags;
    }

    public function addTag(Tag $tag): static
    {
        if (!$this->tags->contains($tag)) {
            $this->tags->add($tag);
        }

        return $this;
    }

    public function removeTag(Tag $tag): static
    {
        $this->tags->removeElement($tag);

        return $this;
    }

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

    public function addTask(Task $task): static
    {
        if (!$this->tasks->contains($task)) {
            $this->tasks->add($task);
            $task->setProject($this);
        }

        return $this;
    }

    public function removeTask(Task $task): static
    {
        if ($this->tasks->removeElement($task)) {
            // set the owning side to null (unless already changed)
            if ($task->getProject() === $this) {
                $task->setProject(null);
            }
        }

        return $this;
    }

    public function getCreatedBy(): ?User
    {
        return $this->createdBy;
    }

    public function setCreatedBy(?User $createdBy): static
    {
        $this->createdBy = $createdBy;

        return $this;
    }

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

    public function setCreatedAt(\DateTimeImmutable $createdAt): static
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getHashtags(): ?string
    {
        return $this->hashtags;
    }

    public function setHashtags(?string $hashtags): static
    {
        $this->hashtags = $hashtags;

        return $this;
    }

    public function getSource(): ?string
    {
        return $this->source;
    }

    public function setSource(?string $source): static
    {
        $this->source = $source;

        return $this;
    }

    public function hasImagery(): bool
    {
        return !empty($this->imagery);
    }

    public function getImagery(): ?string
    {
        return $this->imagery;
    }

    public function setImagery(?string $imagery): static
    {
        $this->imagery = $imagery;

        return $this;
    }

    public function getOverpassQuery(): ?string
    {
        return $this->overpassQuery;
    }

    public function setOverpassQuery(?string $overpassQuery): static
    {
        $this->overpassQuery = $overpassQuery;

        return $this;
    }

    public function hasOverpassQuery(): bool
    {
        return is_string($this->overpassQuery) and !empty(trim($this->overpassQuery));
    }

    public function getOverpassResult(): ?string
    {
        return $this->overpassResult;
    }

    public function setOverpassResult(?string $overpassResult): static
    {
        $this->overpassResult = $overpassResult;

        return $this;
    }

    public function hasOverpassResult(): bool
    {
        return is_string($this->overpassResult) and !empty(trim($this->overpassResult));
    }

    public function isOverpassResultOutdated(): bool
    {
        $data = json_decode($this->overpassResult, true);

        if (is_null($data)) {
            throw new \RuntimeException();
        }

        if (!isset($data['osm3s']['timestamp_osm_base'])) {
            throw new \RuntimeException();
        }

        $generatedAt = new \DateTimeImmutable($data['osm3s']['timestamp_osm_base']);
        $now = new \DateTimeImmutable('now');
        $minimum = new \DateInterval('PT5M');

        $isOutdated = ($generatedAt->add($minimum) < $now);

        return $isOutdated;
    }
}
