<?php

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use App\Repository\ForumRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put;

#[ORM\Entity(repositoryClass: ForumRepository::class)]
#[ApiResource(
    paginationItemsPerPage: 10,
    operations: [
        new GetCollection(normalizationContext: ['groups' => 'message:list']),
        new Post(),
        new Get(normalizationContext: ['groups' => 'message:item']),
        new Put(),
        new Patch(),
        new Delete(),
        
        new GetCollection(
            uriTemplate: '/categories/{categorieId}/forums',
            normalizationContext: ['groups' => 'message:list'],
            provider: 'app.forum_by_category_provider'
        ),
    ]
)]   
class Forum
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Groups(['message:list', 'message:item'])]
    private ?string $nom = null;

    #[ORM\Column(length: 500, nullable: true)]
    #[Groups(['message:list', 'message:item'])]
    private ?string $description = null;

    /**
     * @var Collection<int, Message>
     */
    #[ORM\OneToMany(targetEntity: Message::class, mappedBy: 'id_forum')]
    #[Groups(['message:list', 'message:item'])]
    private Collection $messages;

    #[ORM\ManyToOne(inversedBy: 'forums')]
    #[Groups(['message:list', 'message:item'])]
    private ?Categorie $categorie = null;

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

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): static
    {
        $this->nom = $nom;
        return $this;
    }

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

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

    /**
     * @return Collection<int, Message>
     */
    public function getMessages(): Collection
    {
        return $this->messages;
    }

    public function addMessage(Message $message): static
    {
        if (!$this->messages->contains($message)) {
            $this->messages->add($message);
            $message->setIdForum($this);
        }
        return $this;
    }

    public function removeMessage(Message $message): static
    {
        if ($this->messages->removeElement($message)) {
            if ($message->getIdForum() === $this) {
                $message->setIdForum(null);
            }
        }
        return $this;
    }

    public function getCategorie(): ?Categorie
    {
        return $this->categorie;
    }

    public function setCategorie(?Categorie $categorie): static
    {
        $this->categorie = $categorie;
        return $this;
    }
}