<?php

declare(strict_types=1);

namespace App\Entity;

use App\Repository\SectionRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

#[ORM\Entity(repositoryClass: SectionRepository::class)]
#[ORM\Table(name: 'sections')]
#[ORM\UniqueConstraint(name: 'UNIQ_SECTION_ECOLE_SLUG', columns: ['ecole_id', 'slug'])]
#[ORM\HasLifecycleCallbacks]
class Section
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?Ecole $ecole = null;

    #[ORM\Column(length: 160)]
    #[Assert\NotBlank]
    private ?string $nom = null;

    #[ORM\Column(length: 160)]
    #[Assert\NotBlank]
    #[Assert\Regex(pattern: '/^[a-z0-9-]+$/')]
    private ?string $slug = null;

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

    #[ORM\Column(length: 40)]
    private string $type = 'EN_LIGNE';

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

    #[ORM\Column(options: ['default' => true])]
    private bool $active = true;

    #[ORM\Column(options: ['default' => 0])]
    private int $ordre = 0;

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

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

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column(type: Types::DATETIME_IMMUTABLE)]
    private ?\DateTimeImmutable $updatedAt = null;

    public function getId(): ?int { return $this->id; }
    public function getEcole(): ?Ecole { return $this->ecole; }
    public function setEcole(?Ecole $ecole): static { $this->ecole = $ecole; return $this; }
    public function getNom(): ?string { return $this->nom; }
    public function setNom(string $nom): static { $this->nom = trim($nom); return $this; }
    public function getSlug(): ?string { return $this->slug; }
    public function setSlug(string $slug): static { $this->slug = mb_strtolower(trim($slug)); return $this; }
    public function getDescription(): ?string { return $this->description; }
    public function setDescription(?string $description): static { $this->description = $this->nullableTrim($description); return $this; }
    public function getType(): string { return $this->type; }
    public function setType(string $type): static { $this->type = mb_strtoupper(trim($type)); return $this; }
    public function getPublicCible(): ?string { return $this->publicCible; }
    public function setPublicCible(?string $publicCible): static { $this->publicCible = $this->nullableTrim($publicCible); return $this; }
    public function isActive(): bool { return $this->active; }
    public function setActive(bool $active): static { $this->active = $active; return $this; }
    public function getOrdre(): int { return $this->ordre; }
    public function setOrdre(int $ordre): static { $this->ordre = $ordre; return $this; }
    public function getWhatsappGroupId(): ?string { return $this->whatsappGroupId; }
    public function setWhatsappGroupId(?string $whatsappGroupId): static { $this->whatsappGroupId = $this->nullableTrim($whatsappGroupId); return $this; }
    public function getWhatsappGroupInviteLink(): ?string { return $this->whatsappGroupInviteLink; }
    public function setWhatsappGroupInviteLink(?string $whatsappGroupInviteLink): static { $this->whatsappGroupInviteLink = $this->nullableTrim($whatsappGroupInviteLink); return $this; }
    public function getCreatedAt(): ?\DateTimeImmutable { return $this->createdAt; }
    public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; }

    #[ORM\PrePersist]
    public function initializeTimestamps(): void
    {
        $now = new \DateTimeImmutable();
        $this->createdAt ??= $now;
        $this->updatedAt = $now;
    }

    #[ORM\PreUpdate]
    public function refreshUpdatedAt(): void { $this->updatedAt = new \DateTimeImmutable(); }

    private function nullableTrim(?string $value): ?string
    {
        $value = $value !== null ? trim($value) : null;
        return $value !== '' ? $value : null;
    }
}
