<?php

namespace App\Entity;

use App\Repository\PagesRepository;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Sunrise\Slugger\Slugger;

#[ORM\Entity(repositoryClass: PagesRepository::class), ORM\HasLifecycleCallbacks]

class Pages
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id;

    #[ORM\Column(type: 'string', length: 255)]
    private string $name;

    #[ORM\Column(type: 'text')]
    private string $content;

    #[ORM\Column(type: 'datetime_immutable')]
    private ?DateTimeImmutable $createdAt;

    #[ORM\Column(type: 'datetime_immutable', nullable: true)]
    private ?DateTimeImmutable $modifiedAt;

    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'pages')]
    #[ORM\JoinColumn(nullable: false)]
    private User $owner;

    #[ORM\Column(type: 'string', length: 255)]
    private string $slug;

    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 getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

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

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

        return $this;
    }

    public function getModifiedAt(): ?DateTimeImmutable
    {
        return $this->modifiedAt;
    }

    public function setModifiedAt(?DateTimeImmutable $modifiedAt): self
    {
        $this->modifiedAt = $modifiedAt;

        return $this;
    }

    public function getOwner(): ?User
    {
        return $this->owner;
    }

    public function setOwner(?User $owner): self
    {
        $this->owner = $owner;

        return $this;
    }

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

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    #[ORM\PrePersist]
    public function onPrePersist(): void
    {
        $slugger = new Slugger();
        $slug = $slugger->slugify(string: $this->name);
        $this->slug = $slug;

        $this->createdAt = new DateTimeImmutable(datetime: 'now');
    }

    #[ORM\PreUpdate]
    public function onPreUpdate(): void
    {
        $slugger = new Slugger();
        $slug = $slugger->slugify(string: $this->name);
        $this->slug = $slug;

        $this->modifiedAt = new DateTimeImmutable(datetime: 'now');
    }
}
